#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <dirent.h>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
#include <stdbool.h>
#include <sys/stat.h> 
#include "assets/dh.h"

 


LV_FONT_DECLARE(lv_font_siRouHei_16);

#include "assets/posters/poster_wandering_earth.h"
#include "assets/posters/poster_wandering_earth_detail.h"
#include "assets/posters/poster_yolo.h"
#include "assets/posters/poster_yolo_detail.h"
#include "assets/posters/poster_pegasus.h"
#include "assets/posters/poster_pegasus_detail.h"
#include "assets/posters/poster_article20.h"
#include "assets/posters/poster_article20_detail.h"
#include "assets/posters/poster_boonie_bears.h"
#include "assets/posters/poster_boonie_bears_detail.h"
#include "assets/posters/poster_avatar.h"
#include "assets/posters/poster_avatar_detail.h"

#define DISP_BUF_SIZE (128 * 1024)
#define BASE_START_OBJ_DIR "/"

// 列表页海报声明
LV_IMG_DECLARE(poster_wandering_earth);
LV_IMG_DECLARE(poster_yolo);
LV_IMG_DECLARE(poster_pegasus);
LV_IMG_DECLARE(poster_article20);
LV_IMG_DECLARE(poster_boonie_bears);
LV_IMG_DECLARE(poster_avatar);

// 详情页海报声明
LV_IMG_DECLARE(poster_wandering_earth_detail);
LV_IMG_DECLARE(poster_yolo_detail);
LV_IMG_DECLARE(poster_pegasus_detail);
LV_IMG_DECLARE(poster_article20_detail);
LV_IMG_DECLARE(poster_boonie_bears_detail);
LV_IMG_DECLARE(poster_avatar_detail);

static lv_obj_t *kb = NULL;


// 首先定义结构体类型
typedef struct demo_inf
{
    lv_obj_t * main_windows;
    lv_obj_t * dir_list;
    lv_obj_t * little_windows;

} DI, *P_DI;

struct tmp_inf
{
    P_DI p_di;
    char base_dir_path[256];
};

// 然后添加函数声明
P_DI Demo_Init(void);
void pro_login(void);
uint32_t custom_tick_get(void);

// 添加全局变量用于定时器
static bool login_success = false;
static bool register_success = false;

// 创建主画布 初始化
int Show_File_List(const char * obj_dir_path, P_DI p_di);
void Close_Img_Btn(lv_event_t * event);
void Press_Pic_Btn(lv_event_t * event);
void Press_Dir_Btn(lv_event_t * event);


P_DI Demo_Init(void)
{
    P_DI p_di = (P_DI)malloc(sizeof(DI));
    if(p_di == (P_DI)NULL) {
        perror("malloc ...");
        return (P_DI)-1;
    }

    memset(p_di, 0, sizeof(DI));

    p_di->main_windows = lv_obj_create(NULL);

    p_di->dir_list = lv_list_create(p_di->main_windows);

    p_di->little_windows = lv_obj_create(p_di->main_windows);

    lv_obj_set_size(p_di->dir_list, 300, 480); // 设置左边目录列表大小
    lv_obj_set_size(p_di->little_windows, 480, 480);
    lv_obj_set_pos(p_di->dir_list, 0, 10);
    lv_obj_set_pos(p_di->little_windows, 310, 10);

    lv_scr_load(p_di->main_windows);

    return p_di;
}

// 常量定义
#define SEAT_STATE_DIR "seat_states" 
#define MIN_PASSWORD_LENGTH 6
#define MAX_INPUT_LENGTH 32         //最大输入长度
#define ERROR_DISPLAY_TIME 3000  // 3秒
#define SEAT_STATE_FILE "seat_state.txt"    // 添加全局变量来存储座位状态文件路径

// 评分相关函数声明
static void create_rating_screen(uint32_t movie_index);
static void rating_selected_cb(lv_event_t * e);
static void submit_review_cb(lv_event_t * e);
static void view_reviews_cb(lv_event_t * e);
static void create_reviews_list_screen(uint32_t movie_index);
static void back_to_rating_cb(lv_event_t * e);
// 座位管理函数声明
static void load_seat_state(uint32_t movie_index, int show_time_index);
static void save_seat_state(uint32_t movie_index, int show_time_index);
static void initialize_new_showtime_seats(uint32_t movie_index, int show_time_index);
static void refresh_seat_layout(void);
static void initialize_seats(void);
static char current_showtime_id[32] = ""; 
// 工具函数声明
static void show_message(const char *message);
static void create_seat_state_dir(void);
static char* get_seat_state_filename(uint32_t movie_index, int show_time_index);
// 其他界面相关函数声明
static void create_ticket_selection_screen(uint32_t movie_index);
static void create_seat_legend(lv_obj_t *parent);
static void create_seat_layout(lv_obj_t *parent);
static void seat_click_cb(lv_event_t * e);
static void show_message(const char *message);
static void create_seat_state_dir(void);
static char* get_seat_state_filename(uint32_t movie_index, int show_time_index);

// 添加游戏图标事件处理函数声明
static void game_icon_click_cb(lv_event_t * e);
static void game_icon_hover_cb(lv_event_t * e);
static void msgbox_close_timer(lv_timer_t * timer);
static lv_obj_t *current_msgbox = NULL;
static lv_timer_t *msgbox_timer = NULL;
// 在文件顶部添加全局变量声明
static lv_obj_t *login_screen;  // 登录屏幕
static lv_obj_t *main_screen;   // 主界面屏幕
static lv_obj_t *time_label;
static lv_obj_t *date_label;
static lv_obj_t *movie_grid;
static lv_style_t main_style;
static void ad_timer_cb(lv_timer_t * timer); // 添加广告定时器回调声明

static lv_obj_t *seat_container = NULL;
static uint32_t current_movie_index = 0; 
static void initialize_seats(void);

// 全局变量
static lv_obj_t *current_ta = NULL;
static lv_obj_t *account_ta = NULL;
static lv_obj_t *password_ta = NULL;
static lv_obj_t *login_btn = NULL;
static lv_obj_t *register_btn = NULL;
static lv_obj_t *error_label = NULL;
static lv_obj_t *show_password_btn = NULL;
static lv_timer_t *input_timer = NULL;

// 先声明函数原型
static void update_time_cb(lv_timer_t * timer);
static void movie_click_cb(lv_event_t * e);
static void switch_to_main_interface(void);

static void set_scale_anim(void * var, int32_t value);
static void cleanup_after_logout(void);


// 电影信息结构
typedef struct 
{
    const char *title;
    const char *time;
    const char *hall;
    const char *type;
} MovieInfo;

// 账号信息结构体
typedef struct 
{
    char account[32];
    char password[32];
} AccountInfo;




#define MAX_ACCOUNTS 100
#define ACCOUNT_FILE "accounts.dat"
static int read_accounts(AccountInfo accounts[], int max_count);
static int write_account(const AccountInfo *account);
static bool account_exists(const char *account);
static bool verify_account(const char *account, const char *password);

// 广告定时器回调函数
static void ad_timer_cb(lv_timer_t * timer) 
{
    static int current_ad = 0;
    static const char *ad_messages[] = 
    {
        " 周三特惠：所有场次票价五折！",
        " 超值套餐：电影票+爆米花+饮料=19.99元",
        "会员福利：消费积分可兑换免费观影券",
        " 周末促销：指定影片买一赠一",
        "今日头条：热烈庆祝中国抗日战争暨世界反法西斯胜利80周年-东风5C-打击范围覆盖全球",
        "寻人启示：熊大小朋友与父母走丢，请熊大父母到前台领取熊大"
    };
    
    current_ad = (current_ad + 1) % (sizeof(ad_messages) / sizeof(ad_messages[0]));
    
    // 安全地获取广告标签对象
    if (timer && timer->user_data) {
        lv_obj_t * ad_label = (lv_obj_t *)timer->user_data;
        lv_label_set_text(ad_label, ad_messages[current_ad]);
        lv_obj_fade_in(ad_label, 500, 0);
    }
}

// 时间更新定时器回调
static void update_time_cb(lv_timer_t * timer)
{
    time_t rawtime;
    struct tm * timeinfo;
    char time_buffer[64];
    char date_buffer[64];
    
    time(&rawtime);
    timeinfo = localtime(&rawtime);

  // 更新时间
    strftime(time_buffer, sizeof(time_buffer), "%H:%M:%S", timeinfo);
    lv_label_set_text(time_label, time_buffer);
    // 更新日期 - 使用英文格式
    strftime(date_buffer, sizeof(date_buffer), "%A, %B %d, %Y", timeinfo);
    lv_label_set_text(date_label, date_buffer);
}


// 电影详情结构
typedef struct 
{
    const char *title;
    const char *director;
    const char *actors;
    const char *duration;
    const char *release_date;
    const char *description;
    const lv_img_dsc_t *poster;          // 列表页海报（小图）
    const lv_img_dsc_t *detail_poster;   // 详情页海报（大图，不同版本）
    const char *rating;
} MovieDetail;



// 全局变量声明
static lv_obj_t *detail_screen;
static lv_obj_t *back_btn;

// 电影详情信息数组
MovieDetail movie_details[] = 
{
    {
        "流浪地球3",
        "弗兰特·郭",
        "吴京、李学建、沙毅、李宁、安迪·弗兰德",
        "136分钟",
        "2024-02-10",
        "在不久的将来，科学家们发现太阳正在迅速老化和膨胀，有吞噬地球的危险....",
        &poster_wandering_earth,          // 列表页小海报
        &poster_wandering_earth_detail,   // 详情页大海报（不同版本）
        "8.9"
    },
    {
        "热辣滚烫",
        "贾玲",
        "贾玲、雷佳音、张小斐、沙溢、乔杉",
        "128分钟",
        "2024-02-10",
        "长期宅家缺乏社交的杜乐莹，在遇到拳击教练后决定改变人生……",
        &poster_yolo,                     // 列表页小海报
        &poster_yolo_detail,              // 详情页大海报（不同版本）
        "8.5"
    },
    {
        "飞驰人生 2",
        "韩寒",
        "沈腾、范丞丞、尹正、贾冰、张本煜",
        "121分钟",
        "2024-02-10",
        "昔日冠军车手张驰决定重返赛场，挑战新一代年轻车手…...",
        &poster_pegasus,                  // 列表页小海报
        &poster_pegasus_detail,           // 详情页大海报（不同版本）
        "8.2"
    },
    {
        "第二十条",
        "张艺谋",
        "雷佳音、马丽、赵丽颖、高叶、于和伟",
        "141分钟",
        "2024-02-10",
        "检察官在办理正当防卫案件时陷入情理法的艰难抉择……",
        &poster_article20,                // 列表页小海报
        &poster_article20_detail,         // 详情页大海报（不同版本）
        "8.7"
    },
    {
        "熊出没·逆转时空",
        "林汇达",
        "动画配音团队",
        "96分钟",
        "2024-02-10",
        "熊大熊二意外穿越时空，开启拯救旧友新冒险…",
        &poster_boonie_bears,             // 列表页小海报
        &poster_boonie_bears_detail,      // 详情页大海报（不同版本）
        "8.0"
    },
    {
        "阿凡达：水之道",
        "詹姆斯·卡梅隆",
        "萨姆·沃辛顿、佐伊·索尔达娜、西格妮·韦弗、史蒂芬·朗",
        "192分钟",
        "2022-12-16",
        "杰克·萨利与家人共同守护潘多拉星球的瑰丽水世界……",
        &poster_avatar,                   // 列表页小海报
        &poster_avatar_detail,            // 详情页大海报（不同版本）
        "9.1"
    }
};

// 返回按钮回调
static void back_btn_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        // 确保详情屏幕被正确清理
        if(detail_screen && lv_obj_is_valid(detail_screen)) {
            lv_obj_del(detail_screen);
            detail_screen = NULL;
        }
        lv_scr_load(main_screen);
    }
}


// 所有函数声明
static void create_ticket_selection_screen(uint32_t movie_index);
static void create_seat_legend(lv_obj_t *parent);
static void create_seat_layout(lv_obj_t *parent);
static void seat_click_cb(lv_event_t * e);
static void time_selected_cb(lv_event_t * e);
static void update_price_display(lv_obj_t *parent);
static void confirm_ticket_cb(lv_event_t * e);
static void create_confirmation_dialog(uint32_t movie_index);
static void payment_confirmed_cb(lv_event_t * e);
static void close_dialog_cb(lv_event_t * e);
static void show_payment_success(uint32_t movie_index);
static void show_message(const char *message);
static void back_to_ticket_cb(lv_event_t * e);
static void back_to_detail_from_ticket(lv_event_t * e);
static void create_movie_detail_screen(uint32_t movie_index);

static void logout_btn_event_cb(lv_event_t * e);

static void login_success_timer_cb(lv_timer_t *timer);
static void login_error_timer_cb(lv_timer_t *timer);
static void password_error_timer_cb(lv_timer_t *timer);
// 购票相关结构体定义
typedef struct 
{
    const char *time;
    const char *hall;
    int available_seats;
    float price;
    char id[32];  // 唯一标识符，格式: 电影索引_场次索引
} ShowTime;

typedef struct 
{
    int row;
    int col;
    bool is_available;
    bool is_selected;
    bool is_sold;
    char movie_id[32];  // 所属电影场次ID
} Seat;



// 购票全局变量
static lv_obj_t *ticket_screen = NULL;
static lv_obj_t *price_label = NULL;
static ShowTime show_times[] = {
    {"14:30-16:45", "Hall 1", 45, 12.5f, "0_0"},
    {"17:00-19:15", "Hall 1", 32, 12.5f, "0_1"},
    {"19:30-21:45", "Hall 1", 18, 15.0f, "0_2"},
    {"15:00-17:10", "Hall 2", 56, 10.0f, "1_0"},
    {"18:00-20:10", "Hall 2", 24, 12.0f, "1_1"}
};

static Seat **seats = NULL;
static int total_rows = 8;
static int total_cols = 10;
static int selected_show_time = 0;
static int selected_seat_count = 0;
static float total_price = 0.0f;

// 创建座位状态目录的函数
static void create_seat_state_dir(void)
{
    char command[256];
    snprintf(command, sizeof(command), "mkdir -p %s", SEAT_STATE_DIR);
    
    int result = system(command);
    if (result == 0) {
        printf("创建座位状态目录: %s\n", SEAT_STATE_DIR);
    } else {
        printf("无法创建座位状态目录: %s\n", SEAT_STATE_DIR);
    }
}

// 修改座位状态文件名生成函数
static char* get_seat_state_filename(uint32_t movie_index, int show_time_index)
{
    static char filename[64];
    snprintf(filename, sizeof(filename), "%s/movie_%d_show_%d.dat", SEAT_STATE_DIR, movie_index, show_time_index);
    return filename;
}
// 购票按钮回调
static void buy_ticket_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        uint32_t movie_index = (uint32_t)lv_event_get_user_data(e);
        current_movie_index = movie_index; // 确保这里更新了全局变量
        create_ticket_selection_screen(movie_index);
    }
}

// 创建座位选择界面
static void create_ticket_selection_screen(uint32_t movie_index)
{
      current_movie_index = movie_index;
    
    // 初始化当前场次ID
    snprintf(current_showtime_id, sizeof(current_showtime_id), "%d_%d", movie_index, selected_show_time);

     // 先初始化座位数组
    initialize_seats();
   
    // 然后创建界面
    ticket_screen = lv_obj_create(NULL);
    lv_obj_set_size(ticket_screen, 800, 480);
    lv_obj_set_style_bg_color(ticket_screen, lv_color_white(), 0);

    // 返回按钮
    lv_obj_t *back_btn_ticket = lv_btn_create(ticket_screen); 
    lv_obj_set_size(back_btn_ticket, 80, 40);
    lv_obj_set_pos(back_btn_ticket, 20, 20);
    lv_obj_t *back_label = lv_label_create(back_btn_ticket);
    lv_label_set_text(back_label, "返回");
    lv_obj_set_style_text_font(back_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_center(back_label);
   lv_obj_add_event_cb(back_btn_ticket, back_to_detail_from_ticket, LV_EVENT_CLICKED, (void*)(uintptr_t)movie_index);

    // 电影标题
    lv_obj_t *title_label = lv_label_create(ticket_screen);
    lv_label_set_text(title_label, movie_details[movie_index].title);
    lv_obj_set_pos(title_label, 120, 25);

    // 场次选择下拉框
    lv_obj_t *time_dropdown = lv_dropdown_create(ticket_screen);
    lv_obj_set_size(time_dropdown, 200, 40);
    lv_obj_set_pos(time_dropdown, 400, 20);
    

    // 构建场次选项字符串
    char time_options[512] = "";
    for (int i = 0; i < sizeof(show_times)/sizeof(show_times[0]); i++) 
    {
        char option[64];
        snprintf(option, sizeof(option), "%s %s (￥%.1f)", show_times[i].time, show_times[i].hall, (double)show_times[i].price);
        strcat(time_options, option);
        if (i < sizeof(show_times)/sizeof(show_times[0]) - 1) 
        {
            strcat(time_options, "\n");
        }
    }
    lv_dropdown_set_options(time_dropdown, time_options);
    lv_obj_add_event_cb(time_dropdown, time_selected_cb, LV_EVENT_VALUE_CHANGED, NULL);

    // 座位图例
    create_seat_legend(ticket_screen);

    // 创建座位图
    create_seat_layout(ticket_screen);

    // 初始化当前场次的座位
    load_seat_state(movie_index, selected_show_time);

    // 确认购票按钮
lv_obj_t *confirm_btn = lv_btn_create(ticket_screen);
lv_obj_set_size(confirm_btn, 150, 50);
lv_obj_set_pos(confirm_btn, 620, 400);
lv_obj_t *confirm_label = lv_label_create(confirm_btn);
lv_label_set_text(confirm_label, "确认");
lv_obj_set_style_text_font(confirm_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 修正：设置confirm_label的字体
lv_obj_center(confirm_label);
lv_obj_add_event_cb(confirm_btn, confirm_ticket_cb, LV_EVENT_CLICKED, (void*)(uintptr_t)movie_index);
    // 总价显示
    price_label = lv_label_create(ticket_screen);
    lv_label_set_text(price_label, "总价: ￥0.0");
    lv_obj_set_style_text_font(price_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_set_style_text_color(price_label, lv_color_hex(0xFF3B30), 0);
    lv_obj_set_pos(price_label, 450, 410);

    lv_scr_load(ticket_screen);
}

// 创建座位图例
static void create_seat_legend(lv_obj_t *parent)
{
    lv_obj_t *legend = lv_obj_create(parent);
    lv_obj_set_size(legend, 200, 60);
    lv_obj_set_pos(legend, 580, 80);
    lv_obj_set_style_border_width(legend, 0, 0);

    // 可用座位
    lv_obj_t *avail_seat = lv_obj_create(legend);
    lv_obj_set_size(avail_seat, 20, 20);
    lv_obj_set_style_bg_color(avail_seat, lv_color_hex(0x34C759), 0);
    lv_obj_set_style_radius(avail_seat, 4, 0);

    lv_obj_t *avail_label = lv_label_create(legend);
    lv_label_set_text(avail_label, "可用");
    lv_obj_set_style_text_font(avail_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_set_pos(avail_label, 30, 0);

    // 已选座位
    lv_obj_t *selected_seat = lv_obj_create(legend);
    lv_obj_set_size(selected_seat, 20, 20);
    lv_obj_set_pos(selected_seat, 0, 30);
    lv_obj_set_style_bg_color(selected_seat, lv_color_hex(0x007AFF), 0);
    lv_obj_set_style_radius(selected_seat, 4, 0);

    lv_obj_t *selected_label = lv_label_create(legend);
    lv_label_set_text(selected_label, "已选");
    lv_obj_set_style_text_font(selected_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_set_pos(selected_label, 30, 30);
}

// 创建座位布局
static void create_seat_layout(lv_obj_t *parent)
{
    seat_container = lv_obj_create(parent);
    lv_obj_set_size(seat_container, 500, 300);
    lv_obj_set_pos(seat_container, 50, 80);
    lv_obj_set_style_border_width(seat_container, 0, 0);

    // 屏幕标识
    lv_obj_t *screen_label = lv_label_create(seat_container);
    lv_label_set_text(screen_label, "--------- SCREEN ---------");
    lv_obj_set_style_text_color(screen_label, lv_color_hex(0x888888), 0);
    lv_obj_set_style_text_font(screen_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_align(screen_label, LV_ALIGN_TOP_MID, 0, -20);

    // 初始化座位
    initialize_seats(); // 加载保存的座位状态
    
    for (int row = 0; row < 8; row++) {
        for (int col = 0; col < 10; col++) {
            lv_obj_t *seat_btn = lv_btn_create(seat_container);
            lv_obj_set_size(seat_btn, 25, 25);
            lv_obj_set_pos(seat_btn, col * 30 + 100, row * 30 + 30);
            
            // 设置座位颜色和状态
            if (seats[row][col].is_sold) 
            {
                // 已售出的座位
                lv_obj_set_style_bg_color(seat_btn, lv_color_hex(0x888888), 0);
                lv_obj_clear_flag(seat_btn, LV_OBJ_FLAG_CLICKABLE);
            } 
            else if (!seats[row][col].is_available)
            {
                // 不可用的座位
                lv_obj_set_style_bg_color(seat_btn, lv_color_hex(0x888888), 0);
                lv_obj_clear_flag(seat_btn, LV_OBJ_FLAG_CLICKABLE);
            } 
            else 
            {
                // 可用座位
                lv_obj_set_style_bg_color(seat_btn, lv_color_hex(0x34C759), 0);
            }
            
            lv_obj_set_style_radius(seat_btn, 5, 0);
            
            // 座位编号
            char seat_num[4];
            snprintf(seat_num, sizeof(seat_num), "%c%d", 'A' + row, col + 1);
            lv_obj_t *seat_label = lv_label_create(seat_btn);
            lv_label_set_text(seat_label, seat_num);
            lv_obj_set_style_text_color(seat_label, lv_color_white(), 0);
            lv_obj_center(seat_label);
            lv_obj_add_event_cb(seat_btn, seat_click_cb, LV_EVENT_CLICKED, &seats[row][col]);
        }
    }
}
//初始化状态
static void initialize_seats(void)
{
    // 分配内存
    if (seats == NULL) {
        seats = malloc(total_rows * sizeof(Seat *));
        for (int i = 0; i < total_rows; i++) {
            seats[i] = malloc(total_cols * sizeof(Seat));
        }
    }
    
    // 初始化默认值
    for (int row = 0; row < total_rows; row++) {
        for (int col = 0; col < total_cols; col++) {
            seats[row][col].row = row;
            seats[row][col].col = col;
            seats[row][col].is_available = true;
            seats[row][col].is_selected = false;
            seats[row][col].is_sold = false;
            snprintf(seats[row][col].movie_id, sizeof(seats[row][col].movie_id), 
                    "%d_%d", current_movie_index, selected_show_time);
        }
    }
    
    // 加载保存的状态
    load_seat_state(current_movie_index, selected_show_time);
}


// 保存座位状态到文件
static void save_seat_state(uint32_t movie_index, int show_time_index)
{
    char *filename = get_seat_state_filename(movie_index, show_time_index);
    FILE *file = fopen(filename, "wb");
    if (!file) {
        printf("无法保存座位状态文件: %s\n", filename);
        return;
    }
    
    for (int row = 0; row < total_rows; row++) {
        for (int col = 0; col < total_cols; col++) {
            fwrite(&seats[row][col], sizeof(Seat), 1, file);
        }
    }
    
    fclose(file);
    printf("座位状态已保存: %s\n", filename);
}

// 从文件加载座位状态
static void load_seat_state(uint32_t movie_index, int show_time_index)
{
    char *filename = get_seat_state_filename(movie_index, show_time_index);
    FILE *file = fopen(filename, "rb");
    
    if (!file) {
        printf("无保存的座位状态，将使用默认状态: %s\n", filename);
        // 不需要在这里初始化，initialize_seats 已经做了
        return;
    }
    
    // 读取保存的状态
    for (int row = 0; row < total_rows; row++) {
        for (int col = 0; col < total_cols; col++) {
            size_t read = fread(&seats[row][col], sizeof(Seat), 1, file);
            if (read != 1) {
                printf("座位状态文件损坏，使用默认状态\n");
                fclose(file);
                return;
            }
        }
    }
    
    fclose(file);
    printf("座位状态已加载: %s\n", filename);
}

// 修改新场次座位初始化函数
static void initialize_new_showtime_seats(uint32_t movie_index, int show_time_index)
{
    srand(time(NULL) + movie_index + show_time_index);
    
    for (int row = 0; row < total_rows; row++) {
        for (int col = 0; col < total_cols; col++) {
            seats[row][col].row = row;
            seats[row][col].col = col;
            
            // 为不同电影设置不同的座位可用性模式
            switch(movie_index) {
                case 0: // 流浪地球3 - 热门电影，更多座位售出
                    seats[row][col].is_available = (rand() % 10) > 4; // 50%几率可用
                    break;
                case 1: // 热辣滚烫 - 中等热度
                    seats[row][col].is_available = (rand() % 10) > 3; // 60%几率可用
                    break;
                default: // 其他电影
                    seats[row][col].is_available = (rand() % 10) > 2; // 70%几率可用
            }
            
            seats[row][col].is_selected = false;
            seats[row][col].is_sold = false;
            
            // 设置座位所属的电影场次ID
            snprintf(seats[row][col].movie_id, sizeof(seats[row][col].movie_id), 
                    "%d_%d", movie_index, show_time_index);
        }
    }
}
// 座位点击回调
static void seat_click_cb(lv_event_t * e)
{
    Seat *seat = (Seat *)lv_event_get_user_data(e);
    lv_obj_t *btn = lv_event_get_target(e);

    // 验证座位是否属于当前场次
    char current_movie_id[32];
    snprintf(current_movie_id, sizeof(current_movie_id), "%d_%d", 
            current_movie_index, selected_show_time);

    if (strcmp(seat->movie_id, current_movie_id) != 0) {
        printf("座位ID不匹配: %s vs %s\n", seat->movie_id, current_movie_id);
        show_message("该座位不属于当前场次");
        return;
    }
    
    if (seat->is_sold) {
        show_message("该座位已售出");
        return;
    }
    
    if (!seat->is_available) {
        show_message("该座位不可用");
        return;
    }
    
    // 切换选择状态
    seat->is_selected = !seat->is_selected;
    
    if (seat->is_selected) {
        lv_obj_set_style_bg_color(btn, lv_color_hex(0x007AFF), 0);
        selected_seat_count++;
    } else {
        lv_obj_set_style_bg_color(btn, lv_color_hex(0x34C759), 0);
        selected_seat_count--;
    }
    
    // 更新总价
    total_price = selected_seat_count * show_times[selected_show_time].price;
    update_price_display(ticket_screen);
    
    printf("座位 %c%d %s, 已选座位数: %d, 总价: %.1f\n", 
           'A' + seat->row, seat->col + 1,
           seat->is_selected ? "选中" : "取消",
           selected_seat_count, (double)total_price);
}

// 修改支付确认回调函数，标记座位为已售出
static void payment_confirmed_cb(lv_event_t * e)
{
    uint32_t movie_index = (uint32_t)lv_event_get_user_data(e);
    
    // 标记选中的座位为已售出
    for (int row = 0; row < total_rows; row++) {
        for (int col = 0; col < total_cols; col++) {
            if (seats[row][col].is_selected) {
                seats[row][col].is_sold = true;
                seats[row][col].is_available = false;
                seats[row][col].is_selected = false;
                
                // 确保座位 ID 与当前场次匹配
                snprintf(seats[row][col].movie_id, sizeof(seats[row][col].movie_id), 
                        "%d_%d", movie_index, selected_show_time);
            }
        }
    }
    
    // 保存座位状态
    save_seat_state(movie_index, selected_show_time);
    
    // 关闭对话框
    lv_obj_t *dialog = lv_obj_get_parent(lv_event_get_target(e));
    lv_obj_del(dialog);
    
    show_payment_success(movie_index);
}




// 场次选择回调函数
static void time_selected_cb(lv_event_t * e)
{
    lv_obj_t *dropdown = lv_event_get_target(e);
    int new_show_time = lv_dropdown_get_selected(dropdown);
    
    // 生成新的场次ID
    char new_showtime_id[32];
    snprintf(new_showtime_id, sizeof(new_showtime_id), "%d_%d", current_movie_index, new_show_time);
    
    // 如果切换到了不同的场次，才需要刷新
    if (strcmp(current_showtime_id, new_showtime_id) != 0) {
        // 保存当前场次的座位状态
        if (strlen(current_showtime_id) > 0) {
             save_seat_state(current_movie_index, selected_show_time);
        }
        
        // 更新当前场次信息
        selected_show_time = new_show_time;
        strncpy(current_showtime_id, new_showtime_id, sizeof(current_showtime_id) - 1);
        
        // 加载新场次的座位状态
        load_seat_state(current_movie_index, selected_show_time);
        
        // 重新创建座位布局
        refresh_seat_layout();
        
        // 重置选座状态
        selected_seat_count = 0;
        total_price = 0.0f;
        update_price_display(ticket_screen);
    }
}


// 刷新座位布局函数
static void refresh_seat_layout(void)
{
    // 删除旧的座位容器
    if (seat_container) {
        lv_obj_del(seat_container);
    }
    
    // 重新创建座位布局
    create_seat_layout(ticket_screen);
}

// 更新价格显示
static void update_price_display(lv_obj_t *parent)
{
    if (price_label) {
        char price_text[32];
        snprintf(price_text, sizeof(price_text), "Total: ￥%.1f", (double)total_price);
        lv_label_set_text(price_label, price_text);
    }
}

// 确认购票回调
static void confirm_ticket_cb(lv_event_t * e)
{
    uint32_t movie_index = (uint32_t)lv_event_get_user_data(e);
    
    if (selected_seat_count == 0) {
        show_message("Please select at least one seat!");
        return;
    }
    
    create_confirmation_dialog(movie_index);
}

// 创建确认对话框
static void create_confirmation_dialog(uint32_t movie_index)
{
    lv_obj_t *dialog = lv_obj_create(lv_scr_act());
    lv_obj_set_size(dialog, 400, 300);
    lv_obj_align(dialog, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_bg_color(dialog, lv_color_white(), 0);
    lv_obj_set_style_radius(dialog, 10, 0);
    
    // 标题
    lv_obj_t *title = lv_label_create(dialog);
    lv_label_set_text(title, "确认购买");
    lv_obj_set_style_text_font(title, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 20);
    
    // 订单详情
    char details[256];
    snprintf(details, sizeof(details),
             "电影: %s\n"
             "时长: %s\n"
             "影厅: %s\n"
             "票数: %d\n"
             "金额: ￥%.1f",
             movie_details[movie_index].title,
             show_times[selected_show_time].time,
             show_times[selected_show_time].hall,
             selected_seat_count,
             (double)total_price);
    
    lv_obj_t *details_label = lv_label_create(dialog);
    lv_label_set_text(details_label, details);
    lv_obj_set_style_text_font(details_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_align(details_label, LV_ALIGN_CENTER, 0, -20);
    
    // 确认按钮
    lv_obj_t *confirm_btn = lv_btn_create(dialog);
    lv_obj_set_size(confirm_btn, 100, 40);
    lv_obj_align(confirm_btn, LV_ALIGN_BOTTOM_RIGHT, -20, -20);
    lv_obj_t *confirm_label = lv_label_create(confirm_btn);
    lv_label_set_text(confirm_label, "支付");
     lv_obj_set_style_text_font(confirm_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_center(confirm_label);
    lv_obj_add_event_cb(confirm_btn, payment_confirmed_cb, LV_EVENT_CLICKED, (void*)(uintptr_t)movie_index);
    
    // 取消按钮
    lv_obj_t *cancel_btn = lv_btn_create(dialog);
    lv_obj_set_size(cancel_btn, 100, 40);
    lv_obj_align(cancel_btn, LV_ALIGN_BOTTOM_LEFT, 20, -20);
    lv_obj_t *cancel_label = lv_label_create(cancel_btn);
    lv_label_set_text(cancel_label, "取消");
     lv_obj_set_style_text_font(cancel_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_center(cancel_label);
    lv_obj_add_event_cb(cancel_btn, close_dialog_cb, LV_EVENT_CLICKED, dialog);
}


// 显示支付成功界面
static void show_payment_success(uint32_t movie_index)
{
    lv_obj_t *success_screen = lv_obj_create(NULL);
    lv_obj_set_size(success_screen, 800, 480);
    lv_obj_set_style_bg_color(success_screen, lv_color_hex(0xF8F9FA), 0);
    lv_obj_set_style_pad_all(success_screen, 20, 0);

    // 顶部进度条
    lv_obj_t *progress_container = lv_obj_create(success_screen);
    lv_obj_set_size(progress_container, 700, 60);
    lv_obj_align(progress_container, LV_ALIGN_TOP_MID, 0, 10);
    lv_obj_set_style_border_width(progress_container, 0, 0);
    lv_obj_set_style_bg_color(progress_container, lv_color_hex(0xF8F9FA), 0);

    // 进度步骤
    const char *steps[] = {"选择影片", "选择场次", "选择座位", "支付", "出票完成"};
    lv_coord_t step_width = 140;
    
    for (int i = 0; i < 5; i++) {
        // 步骤圆点
        lv_obj_t *step_dot = lv_obj_create(progress_container);
        lv_obj_set_size(step_dot, 30, 30);
        lv_obj_set_pos(step_dot, i * step_width + 10, 15);
        lv_obj_set_style_radius(step_dot, LV_RADIUS_CIRCLE, 0);
        lv_obj_set_style_bg_color(step_dot, 
            i == 4 ? lv_color_hex(0x34C759) : lv_color_hex(0xE9ECEF), 0);
        lv_obj_set_style_border_width(step_dot, 2, 0);
        lv_obj_set_style_border_color(step_dot, 
            i == 4 ? lv_color_hex(0x28A745) : lv_color_hex(0xDEE2E6), 0);

        // 步骤数字
        lv_obj_t *step_num = lv_label_create(step_dot);
        lv_label_set_text_fmt(step_num, "%d", i + 1);
        lv_obj_set_style_text_color(step_num, 
            i == 4 ? lv_color_white() : lv_color_hex(0x6C757D), 0);
        lv_obj_center(step_num);

        // 步骤文字
        lv_obj_t *step_label = lv_label_create(progress_container);
        lv_label_set_text(step_label, steps[i]);
        lv_obj_set_style_text_font(step_label, &lv_font_siRouHei_16, 0);
        lv_obj_set_style_text_color(step_label, 
            i == 4 ? lv_color_hex(0x34C759) : lv_color_hex(0x6C757D), 0);
        lv_obj_set_pos(step_label, i * step_width + 5, 45);
        lv_obj_set_width(step_label, 130);
        lv_obj_set_style_text_align(step_label, LV_TEXT_ALIGN_CENTER, 0);

        // 连接线（除了最后一个）
        if (i < 4) {
            lv_obj_t *connector = lv_obj_create(progress_container);
            lv_obj_set_size(connector, step_width - 40, 3);
            lv_obj_set_pos(connector, i * step_width + 40, 28);
            lv_obj_set_style_bg_color(connector, 
                i == 3 ? lv_color_hex(0x34C759) : lv_color_hex(0xDEE2E6), 0);
            lv_obj_set_style_border_width(connector, 0, 0);
            lv_obj_set_style_radius(connector, 2, 0);
        }
    }

    // 电影票样式容器
    lv_obj_t *ticket_container = lv_obj_create(success_screen);
    lv_obj_set_size(ticket_container, 650, 280);
    lv_obj_align(ticket_container, LV_ALIGN_CENTER, 0, 30);
    lv_obj_set_style_bg_color(ticket_container, lv_color_white(), 0);
    lv_obj_set_style_radius(ticket_container, 15, 0);
    lv_obj_set_style_shadow_width(ticket_container, 20, 0);
    lv_obj_set_style_shadow_color(ticket_container, lv_color_hex(0x000000), 0);
    lv_obj_set_style_shadow_opa(ticket_container, LV_OPA_20, 0);
    lv_obj_set_style_border_width(ticket_container, 2, 0);
    lv_obj_set_style_border_color(ticket_container, lv_color_hex(0x34C759), 0);

    // 票面顶部装饰线
    lv_obj_t *deco_line = lv_obj_create(ticket_container);
    lv_obj_set_size(deco_line, 650, 5);
    lv_obj_set_pos(deco_line, 0, 0);
    lv_obj_set_style_bg_color(deco_line, lv_color_hex(0x34C759), 0);
    lv_obj_set_style_radius(deco_line, 0, 0);
    lv_obj_set_style_border_width(deco_line, 0, 0);

    // 电影标题
    lv_obj_t *movie_title = lv_label_create(ticket_container);
    lv_label_set_text(movie_title, movie_details[movie_index].title);
    lv_obj_set_style_text_font(movie_title, &lv_font_siRouHei_16, 0);
    lv_obj_set_style_text_color(movie_title, lv_color_hex(0x212529), 0);
    lv_obj_set_pos(movie_title, 20, 20);

    // 场次信息
    lv_obj_t *showtime_info = lv_label_create(ticket_container);
    char showtime_text[128];
    snprintf(showtime_text, sizeof(showtime_text), 
             "场次: %s\n影厅: %s", 
             show_times[selected_show_time].time, 
             show_times[selected_show_time].hall);
    lv_label_set_text(showtime_info, showtime_text);
    lv_obj_set_style_text_font(showtime_info, &lv_font_siRouHei_16, 0);
    lv_obj_set_style_text_color(showtime_info, lv_color_hex(0x495057), 0);
    lv_obj_set_pos(showtime_info, 20, 50);

lv_obj_t *seat_info = lv_label_create(ticket_container);
char seat_text[256] = "座位: ";
int seat_count = 0;
    
// 创建一个临时数组来记录本次购买的座位
Seat purchased_seats[total_rows * total_cols];
int purchased_count = 0;

// 首先收集所有本次购买的座位信息
for (int row = 0; row < total_rows; row++) {
    for (int col = 0; col < total_cols; col++) {
        char current_movie_id[32];
        snprintf(current_movie_id, sizeof(current_movie_id), "%d_%d", 
                movie_index, selected_show_time);
        
        // 检查座位是否属于当前场次且被标记为已售出（本次购买）
        if (strcmp(seats[row][col].movie_id, current_movie_id) == 0 && 
            seats[row][col].is_sold) {
            // 还需要检查这个座位是否是在本次购买中被选中的
            // 添加一个临时标志来标记本次购买的座位
            purchased_seats[purchased_count].row = row;
            purchased_seats[purchased_count].col = col;
            purchased_count++;
        }
    }
}

    // 按行列顺序排序座位（可选，使显示更有序）
// 简单的冒泡排序，按行和列排序
for (int i = 0; i < purchased_count - 1; i++) {
    for (int j = 0; j < purchased_count - i - 1; j++) {
        if (purchased_seats[j].row > purchased_seats[j + 1].row || 
            (purchased_seats[j].row == purchased_seats[j + 1].row && 
             purchased_seats[j].col > purchased_seats[j + 1].col)) {
            Seat temp = purchased_seats[j];
            purchased_seats[j] = purchased_seats[j + 1];
            purchased_seats[j + 1] = temp;
        }
    }
}


 // 然后按顺序显示座位信息
for (int i = 0; i < purchased_count; i++) {
    char seat_num[8];
    snprintf(seat_num, sizeof(seat_num), "%c%d", 
             'A' + purchased_seats[i].row, purchased_seats[i].col + 1);
    
    if (strlen(seat_text) + strlen(seat_num) + 2 < sizeof(seat_text)) {
        if (seat_count > 0) {
            strcat(seat_text, ", ");
        }
        strcat(seat_text, seat_num);
        seat_count++;
    } else {
        strcat(seat_text, "...");
        break;
    }
}

if (seat_count == 0) {
    strcat(seat_text, "无座位信息");
} else {
    // 确保字符串正确终止
    seat_text[sizeof(seat_text) - 1] = '\0';
}

    lv_label_set_text(seat_info, seat_text);
    lv_obj_set_style_text_font(seat_info, &lv_font_siRouHei_16, 0);
    lv_obj_set_style_text_color(seat_info, lv_color_hex(0x495057), 0);
    lv_obj_set_pos(seat_info, 20, 90);

    // 价格信息
    lv_obj_t *price_info = lv_label_create(ticket_container);
    char price_text[64];
    snprintf(price_text, sizeof(price_text), 
             "数量: %d张\n总价: ￥%.1f", 
             selected_seat_count, (double)total_price);
    lv_label_set_text(price_info, price_text);
    lv_obj_set_style_text_font(price_info, &lv_font_siRouHei_16, 0);
    lv_obj_set_style_text_color(price_info, lv_color_hex(0xDC3545), 0);
    lv_obj_set_pos(price_info, 20, 130);

    // 票号信息
    lv_obj_t *ticket_num = lv_label_create(ticket_container);
    char ticket_text[64];
    time_t now = time(NULL);
    snprintf(ticket_text, sizeof(ticket_text), 
             "票号: %ld%04d", now % 100000, rand() % 10000);
    lv_label_set_text(ticket_num, ticket_text);
    lv_obj_set_style_text_font(ticket_num, &lv_font_siRouHei_16, 0);
    lv_obj_set_style_text_color(ticket_num, lv_color_hex(0x6C757D), 0);
    lv_obj_set_pos(ticket_num, 20, 180);

    // 时间信息
    lv_obj_t *time_info = lv_label_create(ticket_container);
    char time_text[64];
    struct tm *tm_info = localtime(&now);
    strftime(time_text, sizeof(time_text), "购票时间: %Y-%m-%d %H:%M:%S", tm_info);
    lv_label_set_text(time_info, time_text);
    lv_obj_set_style_text_font(time_info, &lv_font_siRouHei_16, 0);
    lv_obj_set_style_text_color(time_info, lv_color_hex(0x6C757D), 0);
    lv_obj_set_pos(time_info, 20, 210);

    // 影院信息（右侧）
    lv_obj_t *cinema_info = lv_label_create(ticket_container);
    lv_label_set_text(cinema_info, "星空影院\n自助售票终端");
    lv_obj_set_style_text_font(cinema_info, &lv_font_siRouHei_16, 0);
    lv_obj_set_style_text_color(cinema_info, lv_color_hex(0x34C759), 0);
    lv_obj_set_pos(cinema_info, 450, 20);
    lv_obj_set_style_text_align(cinema_info, LV_TEXT_ALIGN_RIGHT, 0);

    // 成功消息
    lv_obj_t *success_msg = lv_label_create(ticket_container);
    lv_label_set_text(success_msg, "OK 购票成功，祝您观影愉快！");
    lv_obj_set_style_text_font(success_msg, &lv_font_siRouHei_16, 0);
    lv_obj_set_style_text_color(success_msg, lv_color_hex(0x34C759), 0);
    lv_obj_set_pos(success_msg, 450, 180);

    // 返回按钮
    lv_obj_t *back_btn = lv_btn_create(success_screen);
    lv_obj_set_size(back_btn, 200, 50);
    lv_obj_align(back_btn, LV_ALIGN_BOTTOM_MID, 0, -20);
    lv_obj_set_style_bg_color(back_btn, lv_color_hex(0x34C759), 0);
    lv_obj_set_style_radius(back_btn, 10, 0);
    
    lv_obj_t *back_label = lv_label_create(back_btn);
    lv_label_set_text(back_label, "返回首页");
    lv_obj_set_style_text_font(back_label, &lv_font_siRouHei_16, 0);
    lv_obj_set_style_text_color(back_label, lv_color_white(), 0);
    lv_obj_center(back_label);
    
    lv_obj_add_event_cb(back_btn, back_to_detail_from_ticket, LV_EVENT_CLICKED, (void*)(uintptr_t)movie_index);

    lv_scr_load(success_screen);

    // 打印购票信息到控制台
    printf("========== 电影票信息 ==========\n");
    printf("影片: %s\n", movie_details[movie_index].title);
    printf("场次: %s\n", show_times[selected_show_time].time);
    printf("影厅: %s\n", show_times[selected_show_time].hall);
    printf("座位: %s\n", seat_text + 4); // 去掉"座位: "前缀
    printf("数量: %d张\n", selected_seat_count);
    printf("总价: ￥%.1f\n", (double)total_price);
    printf("票号: %s\n", ticket_text + 4); // 去掉"票号: "前缀
    printf("购票时间: %s\n", time_text + 5); // 去掉"购票时间: "前缀
    printf("================================\n");

    // 重置购票状态
    selected_seat_count = 0;
    total_price = 0.0f;
}



// 显示消息
static void show_message(const char *message)
{
    lv_obj_t *msgbox = lv_msgbox_create(NULL, "Info", message, NULL, true);
    lv_obj_center(msgbox);
}

// 关闭对话框
static void close_dialog_cb(lv_event_t * e)
{
    lv_obj_t *dialog = (lv_obj_t *)lv_event_get_user_data(e);
    lv_obj_del(dialog);
}

// 返回详情页
static void back_to_detail_from_ticket(lv_event_t * e)
{
    // 优先从事件参数获取电影索引，如果无效则使用全局变量
    uint32_t movie_index = (uint32_t)lv_event_get_user_data(e);
    
    // 如果从事件参数获取的索引无效，使用全局变量
    if (movie_index >= sizeof(movie_details) / sizeof(movie_details[0])) {
        movie_index = current_movie_index;
    }
    
    printf("DEBUG: 返回详情页，电影索引: %d\n", movie_index);
    
    // 保存当前座位状态
    if (strlen(current_showtime_id) > 0) {
        save_seat_state(movie_index, selected_show_time);
    }
    
    // 重置当前场次信息
    current_showtime_id[0] = '\0';
    
    if (ticket_screen) {
        lv_obj_del(ticket_screen);
        ticket_screen = NULL;
    }
    
    // 更新全局变量以确保一致性
    current_movie_index = movie_index;
    
    // 重新创建详情界面
    create_movie_detail_screen(movie_index);
    lv_scr_load(detail_screen);
    
    printf("DEBUG: 已返回到电影索引 %d 的详情页\n", movie_index);
}


/*评分开始*/



// 在文件顶部添加全局变量
static lv_obj_t *rating_screen = NULL;
static lv_obj_t *reviews_screen = NULL;
static int selected_rating = 0;
static lv_obj_t *comment_ta_global = NULL;
static lv_obj_t *btn_container_global = NULL;
static lv_obj_t *rating_kb = NULL;   // 评分界面键盘

// 评分结构体
typedef struct {
    char username[32];
    int rating;
    char comment[256];
    time_t timestamp;
} MovieReview;

// 模拟历史评价数据
MovieReview movie_reviews[6][5] = {
    { // The Wandering Earth 3
        {"User123", 4, "Amazing visuals and story! Must watch!", 1700000000},
        {"MovieLover", 5, "Great sequel, better than the first one.", 1700086400},
        {"SciFiFan", 5, "Best Chinese sci-fi movie ever!", 1700172800},
        {"", 0, "", 0},
        {"", 0, "", 0}
    },
    { // YOLO
        {"FitnessGuru", 4, "Inspiring story about self-improvement.", 1700000000},
        {"ComedyFan", 3, "Funny and heartwarming at the same time.", 1700086400},
        {"", 0, "", 0},
        {"", 0, "", 0},
        {"", 0, "", 0}
    },
    { // Pegasus 2
        {"RacingFan", 4, "Great racing scenes and comedy.", 1700000000},
        {"", 0, "", 0},
        {"", 0, "", 0},
        {"", 0, "", 0},
        {"", 0, "", 0}
    },
    { // Article 20
        {"LawStudent", 5, "Thought-provoking legal drama.", 1700000000},
        {"", 0, "", 0},
        {"", 0, "", 0},
        {"", 0, "", 0},
        {"", 0, "", 0}
    },
    { // Boonie Bears
        {"FamilyFun", 4, "Kids loved it! Great family movie.", 1700000000},
        {"", 0, "", 0},
        {"", 0, "", 0},
        {"", 0, "", 0},
        {"", 0, "", 0}
    },
    { // Avatar
        {"CinemaLover", 5, "Visual masterpiece! Must see in 3D.", 1700000000},
        {"", 0, "", 0},
        {"", 0, "", 0},
        {"", 0, "", 0},
        {"", 0, "", 0}
    }
};

// 函数声明
static void create_rating_screen(uint32_t movie_index);
static void rating_selected_cb(lv_event_t * e);
static void submit_review_cb(lv_event_t * e);
static void view_reviews_cb(lv_event_t * e);
static void create_reviews_list_screen(uint32_t movie_index);
static void back_to_rating_cb(lv_event_t * e);
static void back_to_detail_from_rating(lv_event_t * e);
static void ta_event_cb(lv_event_t * e);
static void show_message(const char *msg);


static void ta_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * ta = lv_event_get_target(e);
    
    // 添加安全检查，确保键盘对象存在且有效
    if((code == LV_EVENT_CLICKED || code == LV_EVENT_FOCUSED) && rating_kb && lv_obj_is_valid(rating_kb)) {
        printf("显示评分键盘\n");
        
        // 显示评分键盘
        lv_keyboard_set_textarea(rating_kb, ta);
        lv_obj_clear_flag(rating_kb, LV_OBJ_FLAG_HIDDEN);
        
        // 调整界面布局避免键盘遮挡
        if(comment_ta_global && lv_obj_is_valid(comment_ta_global)) {
            lv_obj_align(comment_ta_global, LV_ALIGN_TOP_MID, 0, 250);
        }
        if (btn_container_global && lv_obj_is_valid(btn_container_global)) {
            lv_obj_align(btn_container_global, LV_ALIGN_BOTTOM_MID, 0, 220);
        }
    }
    else if(code == LV_EVENT_DEFOCUSED && rating_kb && lv_obj_is_valid(rating_kb)) {
        printf("隐藏评分键盘\n");
        
        // 隐藏键盘
        lv_keyboard_set_textarea(rating_kb, NULL);
        lv_obj_add_flag(rating_kb, LV_OBJ_FLAG_HIDDEN);
        
        // 恢复界面布局
        if(comment_ta_global && lv_obj_is_valid(comment_ta_global)) {
            lv_obj_align(comment_ta_global, LV_ALIGN_TOP_MID, 0, 280);
        }
        if (btn_container_global && lv_obj_is_valid(btn_container_global)) {
            lv_obj_align(btn_container_global, LV_ALIGN_BOTTOM_MID, 0, -20);
        }
    }
}
// 创建评分界面
static void create_rating_screen(uint32_t movie_index)
{
    current_movie_index = movie_index;
    selected_rating = 0;
    
    rating_screen = lv_obj_create(NULL);
    lv_obj_set_size(rating_screen, 800, 480);
    lv_obj_set_style_bg_color(rating_screen, lv_color_white(), 0);
    lv_obj_set_style_pad_all(rating_screen, 10, 0);

    // 返回按钮
    lv_obj_t *rating_back_btn = lv_btn_create(rating_screen);
    lv_obj_set_size(rating_back_btn, 80, 40);
    lv_obj_set_pos(rating_back_btn, 20, 20);
    lv_obj_t *back_label = lv_label_create(rating_back_btn);
    lv_label_set_text(back_label, "返回");
    lv_obj_set_style_text_font(back_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_center(back_label);
    lv_obj_add_event_cb(rating_back_btn, back_to_detail_from_rating, LV_EVENT_CLICKED, NULL);

    // 标题
    lv_obj_t *title_label = lv_label_create(rating_screen);
    lv_label_set_text(title_label, "给这部电影打分");
    lv_obj_set_style_text_font(title_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_align(title_label, LV_ALIGN_TOP_MID, 0, 30);

    // 电影标题
    lv_obj_t *movie_title = lv_label_create(rating_screen);
    lv_label_set_text(movie_title, movie_details[movie_index].title);
    lv_obj_align(movie_title, LV_ALIGN_TOP_MID, 0, 70);

    // 评分标签
    lv_obj_t *rating_text = lv_label_create(rating_screen);
    lv_label_set_text(rating_text, "点击圆圈进行评分(1-5)");
    lv_obj_set_style_text_font(rating_text, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_set_style_text_color(rating_text, lv_color_hex(0x666666), 0);
    lv_obj_align(rating_text, LV_ALIGN_TOP_MID, 0, 110);

    // 评分圆圈容器
    lv_obj_t *rating_container = lv_obj_create(rating_screen);
    lv_obj_set_size(rating_container, 350, 60);
    lv_obj_align(rating_container, LV_ALIGN_TOP_MID, 0, 140);
    lv_obj_set_style_border_width(rating_container, 0, 0);
    lv_obj_set_style_bg_color(rating_container, lv_color_white(), 0);

    // 创建5个评分圆圈
    lv_obj_t *circles[5];
    for (int i = 0; i < 5; i++) {
        circles[i] = lv_obj_create(rating_container);
        lv_obj_set_size(circles[i], 40, 40);
        lv_obj_set_pos(circles[i], i * 70, 10);
        lv_obj_set_style_radius(circles[i], LV_RADIUS_CIRCLE, 0);
        lv_obj_set_style_bg_color(circles[i], lv_color_hex(0xE0E0E0), 0);
        lv_obj_set_style_border_width(circles[i], 2, 0);
        lv_obj_set_style_border_color(circles[i], lv_color_hex(0xCCCCCC), 0);
        
        // 圆圈内的数字
        lv_obj_t *number_label = lv_label_create(circles[i]);
        char num_text[4];
        snprintf(num_text, sizeof(num_text), "%d", i + 1);
        lv_label_set_text(number_label, num_text);
        lv_obj_set_style_text_color(number_label, lv_color_black(), 0);
        lv_obj_center(number_label);
        
        lv_obj_add_event_cb(circles[i], rating_selected_cb, LV_EVENT_CLICKED, (void*)(intptr_t)(i + 1));
    }

    // 当前评分显示
    lv_obj_t *current_rating_label = lv_label_create(rating_screen);
    lv_label_set_text(current_rating_label, "您的评分：尚未评分");
    lv_obj_set_style_text_font(current_rating_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_set_style_text_color(current_rating_label, lv_color_hex(0x666666), 0);
    lv_obj_set_style_text_font(current_rating_label, &lv_font_montserrat_14, 0);
    lv_obj_align(current_rating_label, LV_ALIGN_TOP_MID, 0, 210);
    lv_obj_set_user_data(rating_screen, current_rating_label);

    // 评论输入框标签
    lv_obj_t *comment_label = lv_label_create(rating_screen);
    lv_label_set_text(comment_label, "您的评论:");
    lv_obj_set_style_text_font(comment_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_align(comment_label, LV_ALIGN_TOP_LEFT, 50, 250);

    // 评论输入框
    lv_obj_t *comment_ta = lv_textarea_create(rating_screen);
    lv_obj_set_size(comment_ta, 700, 80);
    lv_obj_align(comment_ta, LV_ALIGN_TOP_MID, 0, 280);
    lv_textarea_set_placeholder_text(comment_ta, "分享一下你对这部电影的看法...");
    lv_obj_set_style_text_font(comment_ta, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_textarea_set_max_length(comment_ta, 200);
    lv_textarea_set_one_line(comment_ta, false);
    lv_obj_add_event_cb(comment_ta, ta_event_cb, LV_EVENT_ALL, NULL);
    comment_ta_global = comment_ta;

    // 按钮容器
    lv_obj_t *btn_container = lv_obj_create(rating_screen);
    btn_container_global = btn_container;
    lv_obj_set_size(btn_container, 500, 60);
    lv_obj_align(btn_container, LV_ALIGN_BOTTOM_MID, 0, -20);
    lv_obj_set_style_border_width(btn_container, 0, 0);
    lv_obj_set_style_bg_color(btn_container, lv_color_white(), 0);

    // 提交按钮
    lv_obj_t *submit_btn = lv_btn_create(btn_container);
    lv_obj_set_size(submit_btn, 150, 50);
    lv_obj_set_pos(submit_btn, 0, 5);
    lv_obj_t *submit_label = lv_label_create(submit_btn);
    lv_label_set_text(submit_label, "提交评价");
    lv_obj_set_style_text_font(submit_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_center(submit_label);
    lv_obj_add_event_cb(submit_btn, submit_review_cb, LV_EVENT_CLICKED, comment_ta);

    // 查看评价按钮
    lv_obj_t *view_reviews_btn = lv_btn_create(btn_container);
    lv_obj_set_size(view_reviews_btn, 150, 50);
    lv_obj_set_pos(view_reviews_btn, 170, 5);
    lv_obj_t *view_label = lv_label_create(view_reviews_btn);
    lv_label_set_text(view_label, "查看评论");
    lv_obj_set_style_text_font(view_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_center(view_label);
    lv_obj_add_event_cb(view_reviews_btn, view_reviews_cb, LV_EVENT_CLICKED, NULL);

   // 创建键盘（初始隐藏） - 确保键盘是评分屏幕的子对象
rating_kb = lv_keyboard_create(rating_screen);  // 注意这里使用 rating_screen 作为父对象
lv_obj_set_size(rating_kb, 800, 200);
lv_obj_align(rating_kb, LV_ALIGN_BOTTOM_MID, 0, 0);
lv_obj_add_flag(rating_kb, LV_OBJ_FLAG_HIDDEN);
lv_keyboard_set_textarea(rating_kb, NULL);

    lv_scr_load(rating_screen);
}

// 评分选择回调
static void rating_selected_cb(lv_event_t * e)
{
    lv_obj_t *circle = lv_event_get_target(e);
    selected_rating = (int)(intptr_t)lv_event_get_user_data(e);
    
    // 获取所有圆圈
    lv_obj_t *container = lv_obj_get_parent(circle);
    
    for (int i = 0; i < 5; i++) {
        lv_obj_t *current_circle = lv_obj_get_child(container, i);
        
        if (i < selected_rating) {
            // 选中的圆圈
            lv_obj_set_style_bg_color(current_circle, lv_color_hex(0x007AFF), 0);
            lv_obj_set_style_border_color(current_circle, lv_color_hex(0x0055CC), 0);
        } else {
            // 未选中的圆圈
            lv_obj_set_style_bg_color(current_circle, lv_color_hex(0xE0E0E0), 0);
            lv_obj_set_style_border_color(current_circle, lv_color_hex(0xCCCCCC), 0);
        }
    }
    
    // 更新当前评分显示
    lv_obj_t *rating_label = (lv_obj_t *)lv_obj_get_user_data(rating_screen);
    char rating_text[32];
    snprintf(rating_text, sizeof(rating_text), "Your rating: %d/5", selected_rating);
    lv_label_set_text(rating_label, rating_text);
    lv_obj_set_style_text_color(rating_label, lv_color_hex(0x007AFF), 0);
}

static void submit_review_cb(lv_event_t * e)
{
    lv_obj_t *comment_ta = (lv_obj_t *)lv_event_get_user_data(e);
    const char *comment = lv_textarea_get_text(comment_ta);
    
    if (selected_rating == 0) {
        show_message("Please select a rating first!");
        return;
    }
    
    if (strlen(comment) == 0) {
        show_message("Please write a comment!");
        return;
    }
    
    // 保存评价
    for (int i = 0; i < 5; i++)
    {
        if (movie_reviews[current_movie_index][i].rating == 0) 
        {
            strncpy(movie_reviews[current_movie_index][i].username, "You", 32);
            movie_reviews[current_movie_index][i].rating = selected_rating;
            strncpy(movie_reviews[current_movie_index][i].comment, comment, 256);
            movie_reviews[current_movie_index][i].timestamp = time(NULL);
            break;
        }
    }
    
    show_message("Thank you for your review!");
    lv_textarea_set_text(comment_ta, "");
    
    // 重置评分
    selected_rating = 0;
    lv_obj_t *container = lv_obj_get_child(rating_screen, 4);
    for (int i = 0; i < 5; i++) {
        lv_obj_t *circle = lv_obj_get_child(container, i);
        lv_obj_set_style_bg_color(circle, lv_color_hex(0xE0E0E0), 0);
        lv_obj_set_style_border_color(circle, lv_color_hex(0xCCCCCC), 0);
    }
    
    // 重置评分显示
    lv_obj_t *rating_label = (lv_obj_t *)lv_obj_get_user_data(rating_screen);
    lv_label_set_text(rating_label, "您的评分：尚未评分");
     lv_obj_set_style_text_font(rating_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_set_style_text_color(rating_label, lv_color_hex(0x666666), 0);
    
    // 隐藏键盘（添加安全检查）
    if(rating_kb && lv_obj_is_valid(rating_kb)) {
        lv_keyboard_set_textarea(rating_kb, NULL);
        lv_obj_add_flag(rating_kb, LV_OBJ_FLAG_HIDDEN);
    }
}

// 查看评价回调
static void view_reviews_cb(lv_event_t * e)
{
    create_reviews_list_screen(current_movie_index);
}

// 创建评价列表界面
static void create_reviews_list_screen(uint32_t movie_index)
{
    reviews_screen = lv_obj_create(NULL);
    lv_obj_set_size(reviews_screen, 800, 480);
    lv_obj_set_style_bg_color(reviews_screen, lv_color_white(), 0);

    // 返回按钮
    lv_obj_t *reviews_back_btn = lv_btn_create(reviews_screen);
    lv_obj_set_size(reviews_back_btn, 80, 40);
    lv_obj_set_pos(reviews_back_btn, 20, 20);
    lv_obj_t *back_label = lv_label_create(reviews_back_btn);
    lv_label_set_text(back_label, "返回");
    lv_obj_set_style_text_font(back_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_center(back_label);
    lv_obj_add_event_cb(reviews_back_btn, back_to_rating_cb, LV_EVENT_CLICKED, NULL);

    // 标题
    lv_obj_t *title_label = lv_label_create(reviews_screen);
    lv_label_set_text(title_label, "用户评价");
    lv_obj_set_style_text_font(title_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_align(title_label, LV_ALIGN_TOP_MID, 0, 30);

    // 电影标题
    lv_obj_t *movie_title = lv_label_create(reviews_screen);
    lv_label_set_text(movie_title, movie_details[movie_index].title);
    lv_obj_align(movie_title, LV_ALIGN_TOP_MID, 0, 70);

    // 创建滚动容器
    lv_obj_t *scroll = lv_obj_create(reviews_screen);
    lv_obj_set_size(scroll, 700, 320);
    lv_obj_set_pos(scroll, 50, 120);
    lv_obj_set_style_border_width(scroll, 0, 0);

    // 添加评价到滚动容器
    int review_count = 0;
    int y_pos = 10;
    
    for (int i = 0; i < 5; i++) {
        if (movie_reviews[movie_index][i].rating > 0) {
            // 创建评价容器
            lv_obj_t *review_cont = lv_obj_create(scroll);
            lv_obj_set_size(review_cont, 650, 80);
            lv_obj_set_pos(review_cont, 10, y_pos);
            lv_obj_set_style_border_width(review_cont, 1, 0);
            lv_obj_set_style_border_color(review_cont, lv_color_hex(0xE0E0E0), 0);
            lv_obj_set_style_radius(review_cont, 8, 0);
            lv_obj_set_style_pad_all(review_cont, 10, 0);

            // 用户和评分信息
            char header_text[64];
            char time_str[32];
            struct tm *timeinfo = localtime(&movie_reviews[movie_index][i].timestamp);
            strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M", timeinfo);
            
            snprintf(header_text, sizeof(header_text), "%s • %d/5 • %s", 
                    movie_reviews[movie_index][i].username,
                    movie_reviews[movie_index][i].rating,
                    time_str);
            
            lv_obj_t *header_label = lv_label_create(review_cont);
            lv_label_set_text(header_label, header_text);
            lv_obj_set_style_text_color(header_label, lv_color_hex(0x666666), 0);
            lv_obj_set_style_text_font(header_label, &lv_font_montserrat_14, 0);
            lv_obj_set_pos(header_label, 10, 5);

            // 评论内容
            lv_obj_t *comment_label = lv_label_create(review_cont);
            lv_label_set_text(comment_label, movie_reviews[movie_index][i].comment);
            lv_obj_set_width(comment_label, 620);
            lv_obj_set_pos(comment_label, 10, 25);
            lv_label_set_long_mode(comment_label, LV_LABEL_LONG_WRAP);
            
            y_pos += 90;
            review_count++;
        }
    }
    
    if (review_count == 0) {
        lv_obj_t *no_reviews = lv_label_create(reviews_screen);
        lv_label_set_text(no_reviews, "暂无评论，快去成为第一个评论的人！");
         lv_obj_set_style_text_font(no_reviews, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
        lv_obj_align(no_reviews, LV_ALIGN_CENTER, 0, 0);
    }

    lv_scr_load(reviews_screen);
}

// 返回评分界面
static void back_to_rating_cb(lv_event_t * e)
{
    if (reviews_screen) {
        lv_obj_del(reviews_screen);
        reviews_screen = NULL;
    }
    lv_scr_load(rating_screen);
}

static void back_to_detail_from_rating(lv_event_t * e)
{
    printf("从评分返回详情页\n");
    
    // 首先隐藏并解除键盘关联
    if(rating_kb) {
        lv_keyboard_set_textarea(rating_kb, NULL);
        lv_obj_add_flag(rating_kb, LV_OBJ_FLAG_HIDDEN);
    }
    
    // 删除评分屏幕（这会自动删除其所有子对象，包括键盘）
    if (rating_screen) {
        lv_obj_del(rating_screen);
        rating_screen = NULL;
    }
    
    // 不要单独删除键盘，因为它是评分屏幕的子对象，已经随父对象被删除
    // 只需要将指针设置为NULL
    rating_kb = NULL;
    
    // 加载详情屏幕
    lv_scr_load(detail_screen);
}

// 修改评分按钮回调函数
static void rate_movie_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        uint32_t movie_index = (uint32_t)lv_event_get_user_data(e);
        printf("评分按钮被点击，电影索引: %d\n", movie_index);
        create_rating_screen(movie_index);
    }
}


/*评分结束*/


// 创建电影详情界面
static void create_movie_detail_screen(uint32_t movie_index)
{
   // 创建详情屏幕
    detail_screen = lv_obj_create(NULL);
    lv_obj_set_size(detail_screen, 800, 480);
    lv_obj_set_style_bg_color(detail_screen, lv_color_white(), 0);
    
     // 创建返回按钮
    back_btn = lv_btn_create(detail_screen);
    lv_obj_set_size(back_btn, 80, 40);
    lv_obj_set_pos(back_btn, 20, 20);
    lv_obj_t *back_label = lv_label_create(back_btn);
    lv_label_set_text(back_label, "返回");
    lv_obj_set_style_text_font(back_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_center(back_label);
    lv_obj_add_event_cb(back_btn, back_btn_cb, LV_EVENT_CLICKED, NULL);
    
     // 左侧海报区域 - 使用详情页专属海报
    lv_obj_t *poster_container = lv_obj_create(detail_screen);
    lv_obj_set_size(poster_container, 300, 400);
    lv_obj_set_pos(poster_container, 40, 80);
    lv_obj_set_style_border_width(poster_container, 0, 0);
    lv_obj_set_style_bg_color(poster_container, lv_color_white(), 0);
    
     // 海报图像 - 使用详情页专属海报
    lv_obj_t *poster_img = lv_img_create(poster_container);
    lv_img_set_src(poster_img, movie_details[movie_index].detail_poster);
    lv_obj_align(poster_img, LV_ALIGN_CENTER, 0, 0);
    
    // 右侧信息区域
    lv_obj_t *info_container = lv_obj_create(detail_screen);
    lv_obj_set_size(info_container, 400, 400);
    lv_obj_set_pos(info_container, 360, 80);
    lv_obj_set_style_border_width(info_container, 0, 0);
    lv_obj_set_style_bg_color(info_container, lv_color_white(), 0);
    
    // 电影标题
    lv_obj_t *title_label = lv_label_create(info_container);
    lv_label_set_text(title_label, movie_details[movie_index].title);
    lv_obj_set_style_text_font(title_label, &lv_font_siRouHei_16, LV_PART_MAIN);
    lv_obj_set_pos(title_label, 0, 0);
    
    // 评分
    lv_obj_t *rating_label = lv_label_create(info_container);
    char rating_text[32];
    snprintf(rating_text, sizeof(rating_text), "评分: %s/10", movie_details[movie_index].rating);
    lv_label_set_text(rating_label, rating_text);
    lv_obj_set_style_text_font(rating_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_set_style_text_color(rating_label, lv_color_hex(0xFF9900), 0);
    lv_obj_set_pos(rating_label, 0, 40);
    
    // 导演
    lv_obj_t *director_label = lv_label_create(info_container);
    char director_text[128];
    snprintf(director_text, sizeof(director_text), "导演: %s", movie_details[movie_index].director);
    lv_label_set_text(director_label, director_text);
    lv_obj_set_style_text_font(director_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_set_pos(director_label, 0, 70);

    // 演员
    lv_obj_t *actors_label = lv_label_create(info_container);
    char actors_text[256];
    snprintf(actors_text, sizeof(actors_text), "演员: %s", movie_details[movie_index].actors);
    lv_label_set_text(actors_label, actors_text);
    lv_obj_set_style_text_font(actors_label, &lv_font_siRouHei_16, LV_PART_MAIN);  // 添加字体
    lv_obj_set_pos(actors_label, 0, 100);
    lv_obj_set_width(actors_label, 380);
    lv_label_set_long_mode(actors_label, LV_LABEL_LONG_WRAP);
    
   // 时长和上映日期
    lv_obj_t *info_label = lv_label_create(info_container);
    char info_text[128];
    snprintf(info_text, sizeof(info_text), "时长: %s\n上映时间: %s", 
    movie_details[movie_index].duration, movie_details[movie_index].release_date);
    lv_label_set_text(info_label, info_text);

    // 只对标签对象设置字体（移除对info_text的字体设置）
    lv_obj_set_style_text_font(info_label, &lv_font_siRouHei_16, LV_PART_MAIN);
    lv_obj_set_pos(info_label, 0, 150);
    
    // 电影简介
    lv_obj_t *desc_label = lv_label_create(info_container);
    lv_label_set_text(desc_label, movie_details[movie_index].description);
    lv_obj_set_style_text_font(desc_label, &lv_font_siRouHei_16, LV_PART_MAIN);
    lv_obj_set_pos(desc_label, 0, 200);
    lv_obj_set_width(desc_label, 380);
    lv_label_set_long_mode(desc_label, LV_LABEL_LONG_WRAP);
    
    // 操作按钮区域
    lv_obj_t *btn_container = lv_obj_create(info_container);
    lv_obj_set_size(btn_container, 380, 60);
    lv_obj_set_pos(btn_container, 0, 320);
    lv_obj_set_style_border_width(btn_container, 0, 0);
    lv_obj_set_style_bg_color(btn_container, lv_color_white(), 0);
    
    // 购票按钮
lv_obj_t *buy_btn = lv_btn_create(btn_container);
lv_obj_set_size(buy_btn, 120, 40);
lv_obj_set_pos(buy_btn, 0, 10);
lv_obj_t *buy_label = lv_label_create(buy_btn);
lv_label_set_text(buy_label, "购票");
lv_obj_center(buy_label);
lv_obj_set_style_text_font(buy_label, &lv_font_siRouHei_16, LV_PART_MAIN);
lv_obj_add_event_cb(buy_btn, buy_ticket_cb, LV_EVENT_CLICKED, (void*)(uintptr_t)movie_index);
    
    // 评分按钮
    lv_obj_t *rate_btn = lv_btn_create(btn_container);
    lv_obj_set_size(rate_btn, 120, 40);
    lv_obj_set_pos(rate_btn, 130, 10);
    lv_obj_t *rate_label = lv_label_create(rate_btn);
    lv_label_set_text(rate_label, "评分");
    lv_obj_center(rate_label);
    lv_obj_set_style_text_font(rate_label, &lv_font_siRouHei_16, LV_PART_MAIN);
    lv_obj_add_event_cb(rate_btn, rate_movie_cb, LV_EVENT_CLICKED, (void*)(uintptr_t)movie_index);
}



// 电影选择事件回调
static void movie_click_cb(lv_event_t * e)
{
    uint32_t index = (uint32_t)lv_event_get_user_data(e);
    
    // 电影信息数组
    MovieInfo movies[] = {
        {"流浪地球3", "14:30-16:45", "Hall 1", "科幻"},
        {"热辣滚烫", "15:00-17:10", "Hall 2", "剧情"},
        {"飞驰人生2", "16:30-18:20", "Hall 3", "动作"},
        {"第20条", "18:00-20:15", "Hall IMAX", "plot"},
        {"熊出没之逆转时空", "19:30-21:10", "Hall 4", "动画"},
        {"阿凡达之水之道", "20:00-22:45", "Dolby Theater", "科幻 动作"}
    };
    
    printf("选择电影: %s, 时间: %s, 影厅: %s\n", 
           movies[index].title, movies[index].time, movies[index].hall);
    
    // 创建并显示电影详情界面
    create_movie_detail_screen(index);
    lv_scr_load(detail_screen);
}

// 在main函数或适当的位置添加Collect tickets按钮
static void add_collect_tickets_button(void)
{
    lv_obj_t *collect_btn = lv_btn_create(main_screen);
    lv_obj_set_size(collect_btn, 200, 50);
    lv_obj_align(collect_btn, LV_ALIGN_BOTTOM_MID, 0, -20);
    lv_obj_t *btn_label = lv_label_create(collect_btn);
    lv_label_set_text(btn_label, "Collect tickets");
    lv_obj_center(btn_label);
    
    // 这里可以添加点击事件，但电影选择已经在movie_click_cb中处理
}


static void switch_to_main_interface(void)
{
    if(main_screen) {
        lv_obj_del(main_screen); // 删除旧的主界面（如果存在）
    }
    
    // 创建主屏幕
    main_screen = lv_obj_create(NULL);
    lv_scr_load(main_screen);
    
    // 设置主屏幕样式
    lv_style_init(&main_style);
    lv_style_set_bg_color(&main_style, lv_color_hex(0xf5f5f5));
    lv_obj_add_style(main_screen, &main_style, 0);
    lv_obj_clear_flag(main_screen, LV_OBJ_FLAG_SCROLLABLE);
    
    // 创建顶部标题栏
    lv_obj_t * header = lv_obj_create(main_screen);
    lv_obj_set_size(header, LV_HOR_RES, 70);
    lv_obj_set_style_bg_color(header, lv_color_hex(0x2c3e50), 0);
    lv_obj_set_style_border_width(header, 0, 0);
    lv_obj_set_style_radius(header, 0, 0);
    lv_obj_align(header, LV_ALIGN_TOP_MID, 0, 0);
    
    // 影院logo（用标签模拟）
    lv_obj_t * logo = lv_label_create(header);
    lv_label_set_text(logo, "LKst");
    lv_obj_set_style_text_color(logo, lv_color_white(), 0);
    lv_obj_align(logo, LV_ALIGN_LEFT_MID, 20, 0);
    
    // 标题
    lv_obj_t * title = lv_label_create(header);
    lv_label_set_text(title, "星空影院 - 自助售票终端");
    lv_obj_set_style_text_font(title, &lv_font_siRouHei_16, LV_PART_MAIN);
    lv_obj_set_style_text_color(title, lv_color_white(), 0);
    lv_obj_align_to(title, logo, LV_ALIGN_OUT_RIGHT_MID, 10, 0);

    // 创建广告背景条
    lv_obj_t * ad_bg = lv_obj_create(header);
    if (ad_bg) 
    {
        lv_obj_set_size(ad_bg, 320, 30);
        lv_obj_align_to(ad_bg, title, LV_ALIGN_OUT_BOTTOM_MID, 10, 2);
        lv_obj_set_style_bg_color(ad_bg, lv_color_hex(0x2E8B57), 0);
        lv_obj_set_style_bg_opa(ad_bg, LV_OPA_80, 0);
        lv_obj_set_style_radius(ad_bg, 15, 0);

        // 广告内容
        lv_obj_t * ad_label = lv_label_create(ad_bg);
        if (ad_label) 
        {
            lv_label_set_long_mode(ad_label, LV_LABEL_LONG_SCROLL_CIRCULAR);
            lv_obj_set_width(ad_label, 320);
            lv_label_set_text(ad_label, "今日头条：热烈庆祝中国抗日战争暨世界反法西斯胜利80周年-打到小日本-收复台湾-东风5C-打击范围覆盖全球");
            lv_obj_set_style_text_font(ad_label, &lv_font_siRouHei_16, LV_PART_MAIN);
            lv_obj_set_style_text_color(ad_label, lv_color_white(), 0);
            lv_obj_center(ad_label);

            // 创建广告轮播定时器，并设置用户数据
            lv_timer_t * ad_timer = lv_timer_create(ad_timer_cb, 8000, NULL);
            if (ad_timer) 
            {
                ad_timer->user_data = ad_label; // 关键修复：设置用户数据
            }
        }
    }
    
   // 创建时间显示标签 (只需创建一次)
time_label = lv_label_create(header);
if (time_label)
{
    // 先设置文本
    lv_label_set_text(time_label, "00:00:00");
    // 再设置样式和对齐
    lv_obj_set_style_text_color(time_label, lv_color_white(), 0);
    lv_obj_align(time_label, LV_ALIGN_RIGHT_MID, -120, -8);
}

// 日期显示
date_label = lv_label_create(header);
lv_label_set_text(date_label, "Monday, January 1, 2024");
lv_obj_set_style_text_color(date_label, lv_color_white(), 0);
lv_obj_align_to(date_label, time_label, LV_ALIGN_OUT_BOTTOM_MID, 0, 2);
    
    // 创建选项卡视图
    lv_obj_t * tabview = lv_tabview_create(main_screen, LV_DIR_TOP, 40);
    lv_obj_set_size(tabview, LV_HOR_RES, LV_VER_RES - 110); // 减去顶部和底部的高度
    lv_obj_align(tabview, LV_ALIGN_TOP_MID, 0, 70); // 从顶部偏移70px
    
    // 添加两个选项卡：电影和游戏
    lv_obj_t * movie_tab = lv_tabview_add_tab(tabview, "Movies");
    lv_obj_t * game_tab = lv_tabview_add_tab(tabview, "Games");
    
    // 设置选项卡样式
    lv_obj_set_style_bg_color(lv_tabview_get_content(tabview), lv_color_hex(0xf5f5f5), 0);
    
    // 电影选项卡内容
    // 创建内容区域标题
    lv_obj_t * content_title = lv_label_create(movie_tab);
    lv_label_set_text(content_title, "今日热映-一起来看电影吧！");
    lv_obj_set_style_text_font(content_title, &lv_font_siRouHei_16, LV_PART_MAIN);//启用文字
    lv_obj_set_style_text_color(content_title, lv_color_hex(0x2c3e50), 0);
    lv_obj_align(content_title, LV_ALIGN_TOP_MID, 0, 10);
    
    // 创建电影网格容器
    movie_grid = lv_obj_create(movie_tab);
    lv_obj_set_size(movie_grid, LV_HOR_RES - 40, LV_VER_RES - 210);
    lv_obj_align_to(movie_grid, content_title, LV_ALIGN_OUT_BOTTOM_MID, 0, 15);
    lv_obj_set_flex_flow(movie_grid, LV_FLEX_FLOW_ROW_WRAP);
    lv_obj_set_flex_align(movie_grid, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START);
    lv_obj_set_style_pad_all(movie_grid, 10, 0);
    lv_obj_set_style_bg_color(movie_grid, lv_color_hex(0xf5f5f5), 0);
    lv_obj_set_style_border_width(movie_grid, 0, 0);
    lv_obj_set_style_pad_row(movie_grid, 15, 0);
    lv_obj_set_style_pad_column(movie_grid, 15, 0);
    
    // 电影信息
    MovieInfo movies[] = {
        {"流浪地球3", "14:30-16:45", "Hall 1", "科幻"},
        {"热辣滚烫", "15:00-17:10", "Hall 2", "剧情"},
        {"飞驰人生2", "16:30-18:20", "Hall 3", "动作"},
        {"第20条", "18:00-20:15", "Hall IMAX", "plot"},
        {"熊出没之逆转时空", "19:30-21:10", "Hall 4", "动画"},
        {"阿凡达之水之道", "20:00-22:45", "Dolby Theater", "科幻 动作"}
    };
    
    // 颜色值数组（使用十六进制数值，避免函数调用）
    uint32_t color_values[] = {
        0x3498db, // 蓝色
        0xe74c3c, // 红色
        0x2ecc71, // 绿色
        0xf39c12, // 橙色
        0x9b59b6, // 紫色
        0x1abc9c  // 青绿色
    };
    
    // 创建电影卡片
    for (int i = 0; i < sizeof(movies) / sizeof(movies[0]); i++) 
    {
        // 创建卡片容器
        lv_obj_t * card = lv_obj_create(movie_grid);
        lv_obj_set_size(card, 200, 260);
        lv_obj_set_style_bg_color(card, lv_color_white(), 0);
        lv_obj_set_style_border_width(card, 1, 0);
        lv_obj_set_style_border_color(card, lv_color_hex(0xe0e0e0), 0);
        lv_obj_set_style_radius(card, 10, 0);
        lv_obj_set_style_shadow_width(card, 15, 0);
        lv_obj_set_style_shadow_color(card, lv_color_hex(0x888888), 0);
        lv_obj_set_style_pad_all(card, 5, 0);
        
        // 创建海报区域 - 使用图片
        lv_obj_t * poster = lv_img_create(card);
        lv_obj_set_size(poster, 160, 100);
        lv_obj_align(poster, LV_ALIGN_TOP_MID, 0, 10);
        lv_obj_set_style_radius(poster, 8, 0);

        // 设置图片缩放和抗锯齿
        lv_img_set_zoom(poster, 256); // 100% 缩放
        lv_img_set_antialias(poster, true);

        // 根据电影索引设置不同的海报图片
        switch(i) {
            case 0:
                lv_img_set_src(poster, &poster_wandering_earth); // 流浪地球3海报
                break;
            case 1:
                lv_img_set_src(poster, &poster_yolo); // 热辣滚烫海报
                break;
            case 2:
                lv_img_set_src(poster, &poster_pegasus); // 飞驰人生2海报
                break;
            case 3:
                lv_img_set_src(poster, &poster_article20); // 第二十条海报
                break;
            case 4:
                lv_img_set_src(poster, &poster_boonie_bears); // 熊出没海报
                break;
            case 5:
                lv_img_set_src(poster, &poster_avatar); // 阿凡达海报
                break;
            default: 
            {
                lv_obj_t * fallback = lv_obj_create(card);
                lv_obj_set_size(fallback, 160, 100);
                lv_obj_align(fallback, LV_ALIGN_TOP_MID, 0, 10);
                lv_obj_set_style_bg_color(fallback, lv_color_hex(color_values[i % 6]), 0);
                lv_obj_set_style_radius(fallback, 8, 0);
                
                // 添加电影图标
                lv_obj_t * icon = lv_label_create(fallback);
                lv_label_set_text(icon, "🎬");
                lv_obj_set_style_text_color(icon, lv_color_white(), 0);
                lv_obj_center(icon);
                break;
            }
        }
        
        // 电影标题
        lv_obj_t * title_label = lv_label_create(card);
        lv_label_set_text(title_label, movies[i].title);
        lv_obj_set_style_text_font(title_label, &lv_font_siRouHei_16, LV_PART_MAIN);
        lv_obj_set_style_text_color(title_label, lv_color_black(), 0);
        lv_obj_set_style_text_align(title_label, LV_TEXT_ALIGN_CENTER, 0);

        // 检查文本长度是否超过可用宽度
        lv_point_t text_size;
        lv_txt_get_size(&text_size, movies[i].title, LV_FONT_DEFAULT, 0, 0, LV_COORD_MAX, 0);
        if (text_size.x > 170) 
        {  // 170是可用宽度（180减去一些边距）
            lv_label_set_long_mode(title_label, LV_LABEL_LONG_SCROLL_CIRCULAR);
        } 
        else 
        {
            lv_label_set_long_mode(title_label, LV_LABEL_LONG_WRAP);
        }

        lv_obj_set_width(title_label, 180);  // 设置标签宽度
        lv_obj_align_to(title_label, poster, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
        
        // 放映时间
        lv_obj_t * time_label_card = lv_label_create(card);
        lv_label_set_text_fmt(time_label_card, "电影时长: %s", movies[i].time);
        lv_obj_set_style_text_font(time_label_card, &lv_font_siRouHei_16, LV_PART_MAIN);
        lv_obj_set_style_text_color(time_label_card, lv_color_hex(0x666666), 0);
        lv_obj_align_to(time_label_card, title_label, LV_ALIGN_OUT_BOTTOM_MID, 0, 8);
        
        // 影厅信息
        lv_obj_t * hall_label = lv_label_create(card);
        lv_label_set_text_fmt(hall_label, "影厅: %s", movies[i].hall);
        lv_obj_set_style_text_font(hall_label, &lv_font_siRouHei_16, LV_PART_MAIN);
        lv_label_set_long_mode(hall_label, LV_LABEL_LONG_SCROLL_CIRCULAR);    // 设置循环滚动模式
        lv_obj_set_style_text_color(hall_label, lv_color_hex(0x666666), 0);
        lv_obj_align_to(hall_label, time_label_card, LV_ALIGN_OUT_BOTTOM_MID, 0, 5);
        
        // 电影类型
        lv_obj_t * type_label = lv_label_create(card);
        lv_label_set_text_fmt(type_label, "类型: %s", movies[i].type);
        lv_obj_set_style_text_color(type_label, lv_color_white(), 0);
        lv_obj_set_style_text_font(type_label, &lv_font_siRouHei_16, LV_PART_MAIN);//启用文字
        lv_obj_set_style_text_color(type_label, lv_color_hex(0x888888), 0);
        lv_obj_align_to(type_label, hall_label, LV_ALIGN_OUT_BOTTOM_MID, 0, 5);
        
        // 选择按钮
        lv_obj_t * btn = lv_btn_create(card);
        lv_obj_set_size(btn, 120, 30);  // 稍微加宽按钮以适应文本
        lv_obj_align_to(btn, type_label, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
        lv_obj_set_style_bg_color(btn, lv_color_hex(0xe74c3c), 0);
        lv_obj_set_style_radius(btn, 15, 0);
        lv_obj_set_style_bg_color(btn, lv_color_hex(0x3498db), LV_STATE_PRESSED);

        lv_obj_t * btn_label = lv_label_create(btn);
        lv_label_set_text(btn_label, "Collect tickets");
        lv_label_set_long_mode(btn_label, LV_LABEL_LONG_SCROLL_CIRCULAR);    // 设置循环滚动模式
        lv_obj_set_width(btn_label, 100);  // 设置标签宽度，确保滚动效果
        lv_obj_set_style_text_color(btn_label, lv_color_white(), 0);
        lv_obj_center(btn_label);

        // 添加点击事件
        lv_obj_add_event_cb(btn, movie_click_cb, LV_EVENT_CLICKED, (void*)(uintptr_t)i);
    }
    
 // 游戏选项卡内容
lv_obj_t *game_content = lv_label_create(game_tab);
lv_label_set_text(game_content, "电影还未开始，游戏专区 - 选择一款游戏来玩！");
lv_obj_set_style_text_color(game_content, lv_color_hex(0x2c3e50), 0);
lv_obj_set_style_text_font(game_content, &lv_font_siRouHei_16, LV_PART_MAIN);
lv_obj_align(game_content, LV_ALIGN_TOP_MID, 0, 20);

// 创建2048游戏图标容器
lv_obj_t *game_container = lv_obj_create(game_tab);
lv_obj_set_size(game_container, 200, 220);
lv_obj_align(game_container, LV_ALIGN_CENTER, 0, 0);
lv_obj_set_style_bg_color(game_container, lv_color_white(), 0);
lv_obj_set_style_border_width(game_container, 0, 0);
lv_obj_set_style_radius(game_container, 15, 0);
lv_obj_set_style_shadow_width(game_container, 10, 0);
lv_obj_set_style_shadow_color(game_container, lv_color_hex(0x888888), 0);

// 创建2048游戏图标
lv_obj_t *game_icon = lv_obj_create(game_container);
lv_obj_set_size(game_icon, 100, 100);
lv_obj_align(game_icon, LV_ALIGN_TOP_MID, 0, 15);
lv_obj_set_style_bg_color(game_icon, lv_color_hex(0xEDC22E), 0); // 2048游戏标志性的黄色
lv_obj_set_style_radius(game_icon, 10, 0);

// 添加2048文字
lv_obj_t *icon_text = lv_label_create(game_icon);
lv_label_set_text(icon_text, "2048");
lv_obj_set_style_text_color(icon_text, lv_color_white(), 0);
lv_obj_center(icon_text);

// 游戏名称
lv_obj_t *game_name = lv_label_create(game_container);
lv_label_set_text(game_name, "2048 Game");
lv_obj_set_style_text_color(game_name, lv_color_hex(0x2c3e50), 0);
lv_obj_align_to(game_name, game_icon, LV_ALIGN_OUT_BOTTOM_MID, 0, 15);

// 游戏描述
lv_obj_t *game_desc = lv_label_create(game_container);
lv_label_set_text(game_desc, "滑动以合并数字");
lv_obj_set_style_text_color(game_desc, lv_color_hex(0x666666), 0);
lv_obj_set_style_text_font(game_desc, &lv_font_siRouHei_16, LV_PART_MAIN);
lv_obj_align_to(game_desc, game_name, LV_ALIGN_OUT_BOTTOM_MID, 0, 5);

// 添加点击事件
lv_obj_add_flag(game_container, LV_OBJ_FLAG_CLICKABLE);
lv_obj_add_event_cb(game_container, game_icon_click_cb, LV_EVENT_CLICKED, NULL);

// 添加悬停效果
lv_obj_add_event_cb(game_container, game_icon_hover_cb, LV_EVENT_ALL, NULL);
    
    // 创建底部状态栏
    lv_obj_t * footer = lv_obj_create(main_screen);
    lv_obj_set_size(footer, LV_HOR_RES, 40);
    lv_obj_set_style_bg_color(footer, lv_color_hex(0x34495e), 0);
    lv_obj_set_style_border_width(footer, 0, 0);
    lv_obj_set_style_radius(footer, 0, 0);
    lv_obj_align(footer, LV_ALIGN_BOTTOM_MID, 0, 0);



// 退出登录按钮
lv_obj_t *logout_btn = lv_btn_create(footer);
lv_obj_set_size(logout_btn, 100, 30);
lv_obj_align(logout_btn, LV_ALIGN_RIGHT_MID, -10, 0);
lv_obj_set_style_bg_color(logout_btn, lv_color_hex(0xe74c3c), 0);
lv_obj_set_style_radius(logout_btn, 5, 0);

lv_obj_t *logout_label = lv_label_create(logout_btn);
lv_label_set_text(logout_label, "退出登录");
lv_obj_set_style_text_font(logout_label, &lv_font_siRouHei_16, LV_PART_MAIN);
lv_obj_set_style_text_color(logout_label, lv_color_white(), 0);
lv_obj_center(logout_label);

// 添加退出登录事件回调
lv_obj_add_event_cb(logout_btn, logout_btn_event_cb, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t * footer_text = lv_label_create(footer);
    lv_label_set_text(footer_text, "富强 民主 文明 和谐 自由 平等 公正 法治 爱国 敬业 诚信 友善");
    lv_obj_set_style_text_color(footer_text, lv_color_white(), 0);
    lv_obj_set_style_text_font(footer_text, &lv_font_siRouHei_16, LV_PART_MAIN);
    lv_obj_center(footer_text);
    
    // 启动时间更新定时器
    lv_timer_create(update_time_cb, 1000, NULL);
    
    printf("影院取票机主页面创建完成\n");
}




// 2048游戏图标点击回调
static void game_icon_click_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) 
    {
        printf("2048游戏被点击！\n");
        
        // 如果已有消息框，先关闭它
        if(current_msgbox != NULL && lv_obj_is_valid(current_msgbox)) {
            lv_msgbox_close(current_msgbox);
            current_msgbox = NULL;
        }
        
        // 如果已有定时器，先删除它
        if(msgbox_timer != NULL) {
            lv_timer_del(msgbox_timer);
            msgbox_timer = NULL;
        }
        
        // 创建消息框
        current_msgbox = lv_msgbox_create(NULL, "2048 Game", "2048游戏即将推出，敬请期待！", NULL, true);
        
        // 正确设置消息框的字体
        lv_obj_set_style_text_font(current_msgbox, &lv_font_siRouHei_16, LV_PART_MAIN);
        
        // 居中显示
        lv_obj_center(current_msgbox);
        
        // 添加自动关闭定时器
        msgbox_timer = lv_timer_create(msgbox_close_timer, 2000, current_msgbox);
        lv_timer_set_repeat_count(msgbox_timer, 1);
    }
}



// 退出登录时的清理函数
// 退出登录时的清理函数
static void cleanup_after_logout(void)
{
    printf("执行退出登录清理...\n");
    
    // 先解除所有键盘关联
    if(kb && lv_obj_is_valid(kb)) {
        lv_keyboard_set_textarea(kb, NULL);
        lv_obj_del(kb);  // 使用同步删除
        kb = NULL;
    }
    
    if(rating_kb && lv_obj_is_valid(rating_kb)) {
        lv_keyboard_set_textarea(rating_kb, NULL);
        lv_obj_del(rating_kb);  // 使用同步删除
        rating_kb = NULL;
    }
    
    // 清理定时器
    if(input_timer) {
        lv_timer_del(input_timer);
        input_timer = NULL;
    }
    
    if(msgbox_timer) {
        lv_timer_del(msgbox_timer);
        msgbox_timer = NULL;
    }
    
    // 重置所有相关的全局指针
    current_ta = NULL;
    account_ta = NULL;
    password_ta = NULL;
    login_btn = NULL;
    register_btn = NULL;
    error_label = NULL;
    show_password_btn = NULL;
    comment_ta_global = NULL;
    btn_container_global = NULL;
    current_msgbox = NULL;
    
    // 重置屏幕指针
    detail_screen = NULL;
    ticket_screen = NULL;
    rating_screen = NULL;
    reviews_screen = NULL;
    main_screen = NULL;
    
    // 重置状态变量
    selected_seat_count = 0;
    total_price = 0.0f;
    selected_show_time = 0;
    selected_rating = 0;
    login_success = false;
    register_success = false;
    
    printf("清理完成\n");
}
  


// 退出登录按钮回调
static void logout_btn_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        printf("用户退出登录\n");
        
        // 先处理UI更新
        lv_task_handler();
        usleep(10000);
        
        // 执行清理
        cleanup_after_logout();
        
        // 再次处理UI更新，确保清理完成
        lv_task_handler();
        usleep(10000);
        
        // 重新创建登录界面
        pro_login();
        
        // 最后处理UI更新
        lv_task_handler();
        usleep(10000);
    }
}


// 自动关闭消息框的定时器回调
static void msgbox_close_timer(lv_timer_t * timer)
{
    lv_obj_t * msgbox = timer->user_data;
    
    if(msgbox && lv_obj_is_valid(msgbox)) {
        lv_msgbox_close(msgbox);
    }
    
    // 清理指针
    current_msgbox = NULL;
    msgbox_timer = NULL;
    
    lv_timer_del(timer);
}

// 游戏图标悬停效果
static void game_icon_hover_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * target = lv_event_get_target(e);
    
    if(code == LV_EVENT_PRESSED) 
    {
        printf("游戏图标被按下\n");
        
        // 创建按下时的动画
        lv_anim_t a;
        lv_anim_init(&a);
        lv_anim_set_var(&a, target);
        lv_anim_set_values(&a, 256, 200); // 从100%缩放到78% (更明显的效果)
        lv_anim_set_time(&a, 100);
        lv_anim_set_exec_cb(&a, set_scale_anim); // 设置执行回调
        lv_anim_set_path_cb(&a, lv_anim_path_ease_out); // 设置动画路径
        lv_anim_start(&a);
    } 
    else if(code == LV_EVENT_RELEASED) 
    {
        printf("游戏图标释放\n");
        
        // 创建释放时的动画
        lv_anim_t a;
        lv_anim_init(&a);
        lv_anim_set_var(&a, target);
        lv_anim_set_values(&a, 200, 256); // 从78%恢复到100%
        lv_anim_set_time(&a, 100);
        lv_anim_set_exec_cb(&a, set_scale_anim); // 设置执行回调
        lv_anim_set_path_cb(&a, lv_anim_path_ease_out); // 设置动画路径
        lv_anim_start(&a); // 必须调用start!
    }
}

// 动画回调函数 - 设置缩放
static void set_scale_anim(void * var, int32_t value)
{
    lv_obj_t * obj = (lv_obj_t *)var;
    
    // 检查LVGL版本并选择合适的API
    #if LVGL_VERSION_MAJOR >= 8
    // LVGL 8.0+ 使用新的API - 使用 transform_zoom 而不是 transform_scale
    lv_obj_set_style_transform_zoom(obj, value, 0);
    #else
    // LVGL 7.x 使用旧的API
    lv_obj_set_style_transform_scale(obj, value, LV_STYLE_DEFAULT);
    #endif
}








static void account_ta_event_cb(lv_event_t * e);//账号回调
static void password_ta_event_cb(lv_event_t * e);//密码回调
static void kb_event_cb(lv_event_t * e);//键盘回调
static void login_btn_event_cb(lv_event_t * e);//密码显示/隐藏回调
static void register_btn_event_cb(lv_event_t * e);
static void show_password_btn_event_cb(lv_event_t * e);
static void validate_account_input(void);
static void validate_password_input(void);
static void update_login_button_state(void);
static void show_error(const char *message);
static void hide_error(void);
static void hide_error_timer(lv_timer_t *timer);
static void login_result_timer(lv_timer_t *timer);
static void register_result_timer(lv_timer_t *timer);



void pro_login(void)    //登陆界面制作
{
      
    // 先检查并删除现有的登录屏幕
    if(login_screen && lv_obj_is_valid(login_screen)) {
        lv_obj_del(login_screen);
        login_screen = NULL;
    }
    
    // 创建新的登录屏幕
    login_screen = lv_obj_create(NULL);
    lv_scr_load(login_screen);
    
    // 创建背景图片
    lv_obj_t *bg_img = lv_img_create(login_screen);
    lv_img_set_src(bg_img, &dh);
    lv_obj_set_size(bg_img, 800, 480);
    lv_obj_align(bg_img, LV_ALIGN_TOP_LEFT, 0, 0);
    lv_obj_move_background(bg_img);

    // 创建容器
    lv_obj_t *cont = lv_obj_create(login_screen);
    lv_obj_set_size(cont, 800, 480);
    lv_obj_set_style_bg_opa(cont, LV_OPA_0, LV_PART_MAIN); 
    lv_obj_set_style_border_opa(cont, LV_OPA_0, LV_PART_MAIN);
    lv_obj_set_style_outline_opa(cont, LV_OPA_0, LV_PART_MAIN);

    // 账号标签
    lv_obj_t *label = lv_label_create(cont);//设置标签
    lv_label_set_text(label, "账号:");//标签内容
    lv_obj_set_style_text_font(label, &lv_font_siRouHei_16, LV_PART_MAIN);
    lv_obj_set_pos(label, 200, 140);//标签位置
    lv_obj_set_style_text_color(label, lv_color_white(), LV_PART_MAIN);//标签文本颜色

    // 创建账号输入框
    account_ta = lv_textarea_create(cont);// 创建账号输入框
    lv_obj_set_size(account_ta, 250, 40);//账号输入框大小
    lv_obj_set_pos(account_ta, 300, 140);//账号输入框位置
    lv_textarea_set_placeholder_text(account_ta, "账号输入：");// 设置占位符文本，当输入框为空时显示"Enter username"
    lv_obj_set_style_text_font(account_ta, &lv_font_siRouHei_16, LV_PART_MAIN);
    lv_obj_set_style_bg_opa(account_ta, LV_OPA_30, LV_PART_MAIN);//设置透明度为30
    lv_obj_set_style_text_color(account_ta, lv_color_hex(0x333333), LV_PART_MAIN);// 设置文本颜色为深灰色(#333333)
    lv_obj_set_style_bg_color(account_ta, lv_color_white(), LV_PART_MAIN);//设置背景颜色为白色
    lv_obj_set_style_radius(account_ta, 8, LV_PART_MAIN);//设置半径
    lv_obj_set_style_border_width(account_ta, 1, LV_PART_MAIN);//设置宽度
    lv_obj_set_style_border_color(account_ta, lv_color_hex(0xcccccc), LV_PART_MAIN);//设置宽度颜色
     lv_textarea_set_max_length(account_ta, MAX_INPUT_LENGTH);//设置最大输入长度为32
    // 启用点击并添加事件回调
    lv_obj_add_flag(account_ta, LV_OBJ_FLAG_CLICKABLE);//启用点击功能
    lv_obj_add_event_cb(account_ta, account_ta_event_cb, LV_EVENT_CLICKED, NULL);// 添加点击事件回调
    lv_obj_add_event_cb(account_ta, account_ta_event_cb, LV_EVENT_DEFOCUSED, NULL);//失焦事件（LV_EVENT_DEFOCUSED） 的回调，
   
    // 密码标签
    lv_obj_t *label2 = lv_label_create(cont);//创建标签
    lv_label_set_text(label2, "密码:");//标签内容
    lv_obj_set_pos(label2, 200, 200);//标签位置
    lv_obj_set_style_text_font(label2, &lv_font_siRouHei_16, LV_PART_MAIN);
    lv_obj_set_style_text_color(label2, lv_color_white(), LV_PART_MAIN);//标签文本颜色

    // 创建密码输入框
    password_ta = lv_textarea_create(cont); // 在容器cont中创建文本区域对象作为密码输入框
    lv_obj_set_size(password_ta, 250, 40);  // 设置密码输入框的宽度为250像素，高度为40像素
    lv_obj_set_pos(password_ta, 300, 200);  // 设置密码输入框在容器中的位置：x=300, y=200
    lv_textarea_set_placeholder_text(password_ta, "密码输入："); // 设置占位符文本，当输入框为空时显示"Enter password"
    lv_obj_set_style_text_font(password_ta, &lv_font_siRouHei_16, LV_PART_MAIN);
    lv_textarea_set_password_mode(password_ta, true);    // 启用密码模式，输入内容会被隐藏
    // 检查LVGL版本，使用合适的API
    #if LVGL_VERSION_MAJOR >= 8
    lv_textarea_set_password_mode(password_ta, true);              // 设置密码模式下显示的替代字符为圆点"•"
    #endif
    lv_textarea_set_max_length(password_ta,32);      // 设置最大输入长度为32个字符（MAX_INPUT_LENGTH=32）
    lv_obj_set_style_bg_opa(password_ta, LV_OPA_50, LV_PART_MAIN); // 设置背景不透明度为30%（半透明效果）
    lv_obj_set_style_bg_color(password_ta, lv_color_white(), LV_PART_MAIN);// 设置背景颜色为白色
    lv_obj_set_style_text_color(password_ta, lv_color_hex(0x333333), LV_PART_MAIN);// 设置文本颜色为深灰色(#333333)
    lv_obj_set_style_radius(password_ta, 8, LV_PART_MAIN);  // 设置圆角半径为8像素，使输入框呈现圆角矩形
    lv_obj_set_style_border_width(password_ta, 1, LV_PART_MAIN); // 设置边框宽度为1像素
    lv_obj_set_style_border_color(password_ta, lv_color_hex(0xCCCCCC), LV_PART_MAIN);// 设置边框颜色为浅灰色(#CCCCCC)

    
    // 启用点击并添加事件回调
    lv_obj_add_flag(password_ta, LV_OBJ_FLAG_CLICKABLE); // 启用点击功能
    lv_obj_add_event_cb(password_ta, password_ta_event_cb, LV_EVENT_CLICKED, NULL);// 添加点击事件回调
     lv_obj_add_event_cb(password_ta, password_ta_event_cb, LV_EVENT_DEFOCUSED, NULL);//失焦事件（LV_EVENT_DEFOCUSED） 的回调

    
    // 添加显示/隐藏密码按钮
    show_password_btn = lv_btn_create(cont);//创建按钮
    lv_obj_set_size(show_password_btn, 40, 40);//按钮大小
    lv_obj_set_pos(show_password_btn, 555, 200);//按钮位置
    lv_obj_set_style_bg_opa(show_password_btn, LV_OPA_0, LV_PART_MAIN);//透明度为0
    lv_obj_set_style_border_opa(show_password_btn, LV_OPA_0, LV_PART_MAIN);//边框依然存在，但完全透明（不显示）；
    lv_obj_add_event_cb(show_password_btn, show_password_btn_event_cb, LV_EVENT_CLICKED, NULL);// 添加点击事件回调
    lv_obj_t *eye_icon = lv_label_create(show_password_btn);//设置显示/隐藏密码按钮标签
    lv_label_set_text(eye_icon, LV_SYMBOL_EYE_OPEN);//眼睛图标
    lv_obj_center(eye_icon);//居中
    lv_obj_set_style_text_opa(eye_icon, LV_OPA_50, LV_PART_MAIN);//设置 "眼睛图标标签" 文本透明度

    // 创建错误提示标签
    error_label = lv_label_create(cont);//创建标签
    lv_label_set_text(error_label, "");//标签内容-文本配置：初始空文本
    lv_obj_set_pos(error_label, 300, 240);//标签位置
    lv_obj_set_style_text_color(error_label, lv_color_hex(0xff0000), LV_PART_MAIN);//设置文本颜色
    lv_obj_add_flag(error_label, LV_OBJ_FLAG_HIDDEN);//将 "错误提示标签"（error_label）设置为 "隐藏状态"

    // 创建登录按钮 - 修复变量遮蔽问题
    login_btn = lv_btn_create(cont); // 创建登录按钮（去掉前面的 lv_obj_t *）
    lv_obj_set_size(login_btn, 150, 45);//登录按钮大小
    lv_obj_set_pos(login_btn, 200, 260);//登录按钮位置
    lv_obj_set_style_bg_color(login_btn, lv_color_hex(0x3498db), LV_PART_MAIN);//设置背景颜色
    lv_obj_set_style_radius(login_btn, 10, LV_PART_MAIN);//设置圆角
    lv_obj_add_state(login_btn, LV_STATE_DISABLED);//将登录按钮（login_btn）设置为 "禁用状态" 的核心接口，禁用后按钮会呈现 "不可点击、视觉弱化" 效果
    lv_obj_add_event_cb(login_btn, login_btn_event_cb, LV_EVENT_CLICKED, NULL);// 添加点击事件回调
    lv_obj_t *login_label = lv_label_create(login_btn);//登录按钮文本标签
    lv_label_set_text(login_label, "登录");//标签内容
    lv_obj_set_style_text_font(login_label, &lv_font_siRouHei_16, LV_PART_MAIN);
    lv_obj_center(login_label);//文本居中
    lv_obj_set_style_text_color(login_label, lv_color_white(), LV_PART_MAIN);//文本颜色
    
    // 创建注册按钮 - 修复变量遮蔽问题
    register_btn = lv_btn_create(cont);//创建按钮（去掉前面的 lv_obj_t *）
    lv_obj_set_size(register_btn, 150, 45);//按钮大小
    lv_obj_set_pos(register_btn, 400, 260);//按钮位置
    lv_obj_set_style_bg_color(register_btn, lv_color_hex(0x3498db), LV_PART_MAIN);//设置背景颜色
    lv_obj_set_style_radius(register_btn, 10, LV_PART_MAIN);//设置圆角
    lv_obj_add_event_cb(register_btn, register_btn_event_cb, LV_EVENT_CLICKED, NULL);//添加点击回调
    lv_obj_t *register_label = lv_label_create(register_btn);//创建标签
    lv_label_set_text(register_label, "注册");//标签文本内容
    lv_obj_set_style_text_font(register_label, &lv_font_siRouHei_16, LV_PART_MAIN);
    lv_obj_center(register_label);//标签居中
    lv_obj_set_style_text_color(register_label, lv_color_white(), LV_PART_MAIN);//标签文本颜色

    
    // 创建键盘
  kb = lv_keyboard_create(login_screen);
    lv_obj_set_size(kb, 800, 200);
    lv_obj_align(kb, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_event_cb(kb, kb_event_cb, LV_EVENT_ALL, NULL);

    printf("界面创建完成，已为输入框启用点击功能\n");
}

// 账号输入框事件回调
static void account_ta_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    
    if(code == LV_EVENT_CLICKED) 
    {
        printf("账号输入框被点击！\n");
        current_ta = account_ta;
        
        // 显示键盘并关联到当前输入框
        if(kb && lv_obj_is_valid(kb)) {
            lv_keyboard_set_textarea(kb, current_ta);
            lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
        }
        
        // 给输入框添加焦点状态
        lv_obj_add_state(current_ta, LV_STATE_FOCUSED);
        
        // 视觉反馈：改变边框颜色
        lv_obj_set_style_border_color(current_ta, lv_color_hex(0x3498db), LV_PART_MAIN);
    }
    else if(code == LV_EVENT_DEFOCUSED)
    {
        validate_account_input();
        update_login_button_state();
    }
}


// 密码输入框事件回调
static void password_ta_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    
    if(code == LV_EVENT_CLICKED) 
    {
        printf("密码输入框被点击！\n");
        
        // 安全检查：确保键盘存在且有效
        if(kb == NULL || !lv_obj_is_valid(kb)) {
            printf("键盘未创建或无效，重新创建键盘\n");
            // 重新创建键盘
            kb = lv_keyboard_create(lv_scr_act());
            lv_obj_set_size(kb, 800, 200);
            lv_obj_align(kb, LV_ALIGN_BOTTOM_MID, 0, 0);
            lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
            lv_obj_add_event_cb(kb, kb_event_cb, LV_EVENT_ALL, NULL);
        }
        
        current_ta = password_ta;
        
        // 显示键盘并关联到当前输入框
        lv_keyboard_set_textarea(kb, current_ta);
        lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
        
        // 给输入框添加焦点状态
        lv_obj_add_state(current_ta, LV_STATE_FOCUSED);
        
        // 视觉反馈：改变边框颜色
        lv_obj_set_style_border_color(current_ta, lv_color_hex(0x3498db), LV_PART_MAIN);
    }
    else if(code == LV_EVENT_DEFOCUSED)
    {
        validate_password_input();
        update_login_button_state();
    }
}

// 键盘事件回调
static void kb_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * keyboard = lv_event_get_target(e);
    
    // 安全检查
    if(keyboard == NULL || !lv_obj_is_valid(keyboard)) {
        printf("键盘无效，忽略事件\n");
        return;
    }
    
    if(code == LV_EVENT_READY || code == LV_EVENT_CANCEL) 
    {
        printf("隐藏键盘\n");
        lv_obj_add_flag(keyboard, LV_OBJ_FLAG_HIDDEN);
        
        if(current_ta) 
        {
            // 移除焦点状态
            lv_obj_clear_state(current_ta, LV_STATE_FOCUSED);
            
            // 恢复边框颜色
            if(current_ta == account_ta) 
            {
                lv_obj_set_style_border_color(current_ta, lv_color_hex(0xcccccc), LV_PART_MAIN);
                const char *text = lv_textarea_get_text(account_ta);
                printf("账号: %s\n", text);
                
                // 验证账号输入
                validate_account_input();
            } 
            else if(current_ta == password_ta) 
            {
                lv_obj_set_style_border_color(current_ta, lv_color_hex(0xCCCCCC), LV_PART_MAIN);
                const char *text = lv_textarea_get_text(password_ta);
                printf("密码: %s\n", text);
                
                // 验证密码输入
                validate_password_input();
            }
            
            current_ta = NULL;
        }
        
        // 更新登录按钮状态
        update_login_button_state();
    } 
    else if(code == LV_EVENT_VALUE_CHANGED) {
        // 键盘值改变时实时验证（更适合输入验证）
        if(current_ta == account_ta) {
            validate_account_input();
        } else if(current_ta == password_ta) {
            validate_password_input();
        }
        
        // 更新登录按钮状态
        update_login_button_state();
    }
    else if(code == LV_EVENT_CLICKED) {
        // 检查点击的是否是特殊按钮（如关闭、确认等）
        const char * btn_txt = lv_btnmatrix_get_btn_text(keyboard, lv_btnmatrix_get_selected_btn(keyboard));
        
        if(btn_txt) 
        {
            // 只有特定按钮才隐藏键盘
            if(strcmp(btn_txt, LV_SYMBOL_CLOSE) == 0 || 
               strcmp(btn_txt, LV_SYMBOL_OK) == 0 ||
               strcmp(btn_txt, "Close") == 0 ||
               strcmp(btn_txt, "OK") == 0) {
                printf("隐藏键盘（特殊按钮点击）\n");
                lv_obj_add_flag(keyboard, LV_OBJ_FLAG_HIDDEN);
                
                if(current_ta) 
                {
                    lv_obj_clear_state(current_ta, LV_STATE_FOCUSED);
                    current_ta = NULL;
                }
            }
        }
    }
}


// 登录按钮事件回调
// 登录按钮事件回调
static void login_btn_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    
    if(code == LV_EVENT_CLICKED) 
    {
        const char *account = lv_textarea_get_text(account_ta);
        const char *password = lv_textarea_get_text(password_ta);
        
        // 首先检查输入是否为空
        if(strlen(account) == 0) 
        {
            show_error("请输入账号");
            return;
        }
        
        if(strlen(password) == 0) 
        {
            show_error("请输入密码");
            return;
        }
        
        // 验证账号密码
        AccountInfo accounts[MAX_ACCOUNTS];
        int count = read_accounts(accounts, MAX_ACCOUNTS);
        
        bool account_found = false;
        bool password_correct = false;
        
        // 检查账号是否存在
        for (int i = 0; i < count; i++) 
        {
            if (strcmp(accounts[i].account, account) == 0)
            {
                account_found = true;
                // 检查密码是否正确
                if (strcmp(accounts[i].password, password) == 0) 
                {
                    password_correct = true;
                }
                break;
            }
        }
        
        if (account_found && password_correct) {
            printf("登录成功!\n");
            show_error("登录成功! 正在跳转到主界面...");
            login_success = true;
            
            // 更新按钮状态
            lv_obj_add_state(login_btn, LV_STATE_DISABLED);
            lv_label_set_text(lv_obj_get_child(login_btn, 0), "正在登录...");
            
            // 添加短暂延迟后再切换界面
            lv_timer_create(login_success_timer_cb, 1000, NULL);
            
        } else if (account_found && !password_correct) {
            printf("登录失败: 密码错误!\n");
            show_error("密码错误，请重新输入");
            login_success = false;
            
            // 设置定时器恢复按钮状态
            lv_timer_create(login_result_timer, 1000, NULL);
            
        } else {
            printf("登录失败: 账号未注册!\n");
            show_error("账号未注册，请先注册");
            login_success = false;
            
            // 设置定时器恢复按钮状态
            lv_timer_create(login_result_timer, 1000, NULL);
        }
    }
}

// 登录成功定时器回调
static void login_success_timer_cb(lv_timer_t *timer)
{
    switch_to_main_interface();
    lv_timer_del(timer);
}

// 登录错误定时器回调
static void login_error_timer_cb(lv_timer_t *timer)
{
    // 可以在这里添加错误处理逻辑
    lv_timer_del(timer);
}

// 密码错误定时器回调
static void password_error_timer_cb(lv_timer_t *timer)
{
    // 可以在这里添加错误处理逻辑
    lv_timer_del(timer);
}

// 注册按钮事件回调
static void register_btn_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    
    if(code == LV_EVENT_CLICKED) {
        const char *account = lv_textarea_get_text(account_ta);
        const char *password = lv_textarea_get_text(password_ta);
        
        // 验证输入
        if(strlen(account) == 0) {
            show_error("请输入账号");
            return;
        }
        
        if(strlen(password) < MIN_PASSWORD_LENGTH) {
            char error_msg[64];
            snprintf(error_msg, sizeof(error_msg), "密码太短，至少需要%d位", MIN_PASSWORD_LENGTH);
            show_error(error_msg);
            return;
        }
        
        // 检查账号是否已存在
        if (account_exists(account)) {
            show_error("账号已存在，请使用其他账号");
            return;
        }
        
        printf("尝试注册 - 账号: %s, 密码: %s\n", account, password);
        
        // 保存账号信息
        AccountInfo new_account;
        strncpy(new_account.account, account, sizeof(new_account.account) - 1);
        strncpy(new_account.password, password, sizeof(new_account.password) - 1);
        new_account.account[sizeof(new_account.account) - 1] = '\0';
        new_account.password[sizeof(new_account.password) - 1] = '\0';
        
        if (write_account(&new_account) == 0) {
            printf("注册成功!\n");
            show_error("注册成功! 现在可以登录了");
            register_success = true;
            
            // 清空密码输入框
            lv_textarea_set_text(password_ta, "");
        } else {
            printf("注册失败!\n");
            show_error("注册失败，请重试");
            register_success = false;
        }
        
        // 模拟注册过程
        lv_obj_add_state(register_btn, LV_STATE_DISABLED);
        lv_label_set_text(lv_obj_get_child(register_btn, 0), "正在注册...");
        
        // 设置定时器显示结果
        lv_timer_t *timer = lv_timer_create(register_result_timer, 1000, NULL);
        (void)timer; // 消除未使用变量的警告
    }
}

// 显示/隐藏密码按钮事件回调
static void show_password_btn_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    
    if(code == LV_EVENT_CLICKED) {
        static bool password_visible = false;
        
        if(password_visible) {
            lv_textarea_set_password_mode(password_ta, true);
            lv_obj_set_style_text_opa(show_password_btn, LV_OPA_50, LV_PART_MAIN);
        } else {
            lv_textarea_set_password_mode(password_ta, false);
            lv_obj_set_style_text_opa(show_password_btn, LV_OPA_100, LV_PART_MAIN);
        }
        
        password_visible = !password_visible;
    }
}

// 验证账号输入
static void validate_account_input(void)
{
    const char *text = lv_textarea_get_text(account_ta);
    size_t len = strlen(text);
    
    if(len == 0) 
    {
        lv_obj_set_style_border_color(account_ta, lv_color_hex(0xcccccc), LV_PART_MAIN);
        hide_error();
    } 
    else if(len > MAX_INPUT_LENGTH)
    {
        lv_obj_set_style_border_color(account_ta, lv_color_hex(0xff0000), LV_PART_MAIN);
        show_error("账号过长 (最多32字符)");
    } 
    else 
    {
        // 检查账号是否存在
        if(account_exists(text)) 
        {
            lv_obj_set_style_border_color(account_ta, lv_color_hex(0x00ff00), LV_PART_MAIN);
            show_error("账号已存在，可以登录");
        }
        else 
        {
            lv_obj_set_style_border_color(account_ta, lv_color_hex(0x3498db), LV_PART_MAIN);
            show_error("新账号，可以注册");
        }
    }
}

// 验证密码输入
static void validate_password_input(void)
{
    const char *text = lv_textarea_get_text(password_ta);
    size_t len = strlen(text);
    
    if(len == 0) 
    {
        lv_obj_set_style_border_color(password_ta, lv_color_hex(0xcccccc), LV_PART_MAIN);
        hide_error();
    } 
    else if(len < MIN_PASSWORD_LENGTH) 
    {
        lv_obj_set_style_border_color(password_ta, lv_color_hex(0xff0000), LV_PART_MAIN);
        char msg[64];
        snprintf(msg, sizeof(msg), "密码太短 (至少需要%d位)", MIN_PASSWORD_LENGTH);
        show_error(msg);
    } 
    else 
    {
        lv_obj_set_style_border_color(password_ta, lv_color_hex(0x00ff00), LV_PART_MAIN);
        show_error("密码强度足够");
    }
}

// 更新登录按钮状态
static void update_login_button_state(void)
{
    if(login_btn) {
        const char *account = lv_textarea_get_text(account_ta);
        const char *password = lv_textarea_get_text(password_ta);
        
        if(account && password && 
           strlen(account) > 0 && 
           strlen(password) >= MIN_PASSWORD_LENGTH) {
            // 启用登录按钮
            lv_obj_clear_state(login_btn, LV_STATE_DISABLED);
            lv_obj_set_style_bg_color(login_btn, lv_color_hex(0x3498db), LV_PART_MAIN);
        } else {
            // 禁用登录按钮
            lv_obj_add_state(login_btn, LV_STATE_DISABLED);
            lv_obj_set_style_bg_color(login_btn, lv_color_hex(0x95a5a6), LV_PART_MAIN);
        }
    }
}

// 显示错误信息
static void show_error(const char *message)
{
    if(error_label) 
    {
        lv_label_set_text(error_label, message);
        lv_obj_set_style_text_font(error_label, &lv_font_siRouHei_16, LV_PART_MAIN); // 确保字体设置
        lv_obj_clear_flag(error_label, LV_OBJ_FLAG_HIDDEN);
        
        // 设置定时器隐藏错误信息
        if(input_timer)
        {
            lv_timer_del(input_timer);
        }
        input_timer = lv_timer_create(hide_error_timer, ERROR_DISPLAY_TIME, NULL);
        
        printf("显示错误: %s\n", message); // 在控制台也输出
    }
}


// 隐藏错误信息
static void hide_error(void)
{
    if(error_label) 
    {
        lv_obj_add_flag(error_label, LV_OBJ_FLAG_HIDDEN);
    }
    
    if(input_timer) {
        lv_timer_del(input_timer);
        input_timer = NULL;
    }
}

// 错误信息隐藏定时器
static void hide_error_timer(lv_timer_t *timer)
{
    hide_error();
}

// 登录结果处理
// 登录结果处理
static void login_result_timer(lv_timer_t *timer)
{
    // 只恢复按钮状态，不处理界面切换
    lv_obj_clear_state(login_btn, LV_STATE_DISABLED);
    lv_label_set_text(lv_obj_get_child(login_btn, 0), "登录");
    lv_timer_del(timer);
}


// 注册结果处理
static void register_result_timer(lv_timer_t *timer)
{
    bool success = register_success; // 使用全局变量
    
    if(success) {
        printf("注册成功!\n");
        show_error("Registration successful!");
    } else {
        printf("注册失败!\n");
        show_error("Registration failed - account may exist");
    }
    
    // 恢复注册按钮
    lv_obj_clear_state(register_btn, LV_STATE_DISABLED);
    lv_label_set_text(lv_obj_get_child(register_btn, 0), "注册");
    lv_obj_set_style_text_font(register_btn, &lv_font_siRouHei_16, LV_PART_MAIN);// 应用字体
    lv_timer_del(timer);
}


// 读取所有账号信息
static int read_accounts(AccountInfo accounts[], int max_count) {
    FILE *file = fopen("accounts.txt", "r");
    if (!file) {
        printf("账号文件不存在，将创建新文件\n");
        return 0; // 文件不存在
    }
    
    int count = 0;
    char line[256];
    while (count < max_count && fgets(line, sizeof(line), file)) {
        // 解析每行: 账号:密码
        char *colon = strchr(line, ':');
        if (colon) {
            *colon = '\0'; // 分割字符串
            strncpy(accounts[count].account, line, sizeof(accounts[count].account) - 1);
            strncpy(accounts[count].password, colon + 1, sizeof(accounts[count].password) - 1);
            
            // 去除密码末尾的换行符
            char *newline = strchr(accounts[count].password, '\n');
            if (newline) *newline = '\0';
            
            count++;
        }
    }
    
    fclose(file);
    return count;
}

// 写入账号信息的函数
static int write_account(const AccountInfo *account) {
    FILE *file = fopen("accounts.txt", "a"); // 追加模式
    if (!file) {
        printf("无法打开账号文件\n");
        return -1;
    }
    
    fprintf(file, "%s:%s\n", account->account, account->password);
    fclose(file);
    return 0;
}

// 检查账号是否已存在
static bool account_exists(const char *account) {
    AccountInfo accounts[MAX_ACCOUNTS];
    int count = read_accounts(accounts, MAX_ACCOUNTS);
    
    for (int i = 0; i < count; i++)
    {
        if (strcmp(accounts[i].account, account) == 0) {
            return true;
        }
    }
    return false;
}


// 验证账号密码
static bool verify_account(const char *account, const char *password) {
    AccountInfo accounts[MAX_ACCOUNTS];
    int count = read_accounts(accounts, MAX_ACCOUNTS);
    
    for (int i = 0; i < count; i++) {
        if (strcmp(accounts[i].account, account) == 0 && 
            strcmp(accounts[i].password, password) == 0) {
            return true;
        }
    }
    return false;
}



int main(void)
{
    /*LittlevGL init*/
    lv_init();

    /*Linux frame buffer device init*/
    fbdev_init();

    /*A small buffer for LittlevGL to draw the screen's content*/
    static lv_color_t buf[DISP_BUF_SIZE];

    /*Initialize a descriptor for the buffer*/
    static lv_disp_draw_buf_t disp_buf;
    lv_disp_draw_buf_init(&disp_buf, buf, NULL, DISP_BUF_SIZE);

    /*Initialize and register a display driver*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf = &disp_buf;
    disp_drv.flush_cb = fbdev_flush;
    disp_drv.hor_res  = 800;
    disp_drv.ver_res  = 480;
    lv_disp_drv_register(&disp_drv);

    evdev_init();
    static lv_indev_drv_t indev_drv_1;
    lv_indev_drv_init(&indev_drv_1); /*Basic initialization*/
    indev_drv_1.type = LV_INDEV_TYPE_POINTER;

    /*This function will be called periodically (by the library) to get the mouse position and state*/
    indev_drv_1.read_cb      = evdev_read;
    lv_indev_t * mouse_indev = lv_indev_drv_register(&indev_drv_1);

    /*Set a cursor for the mouse*/
    LV_IMG_DECLARE(mouse_cursor_icon)
    lv_obj_t * cursor_obj = lv_img_create(lv_scr_act()); /*Create an image object for the cursor */
    lv_img_set_src(cursor_obj, &mouse_cursor_icon);      /*Set the image source*/
    lv_indev_set_cursor(mouse_indev, cursor_obj);        /*Connect the image  object to the driver*/

    create_seat_state_dir();
    pro_login();

    /*Handle LitlevGL tasks (tickless mode)*/
    while(1) {
        lv_timer_handler();
        usleep(5000);
    }

    return 0;
}

/*Set in lv_conf.h as `LV_TICK_CUSTOM_SYS_TIME_EXPR`*/
uint32_t custom_tick_get(void)
{
    static uint64_t start_ms = 0;
    if(start_ms == 0) {
        struct timeval tv_start;
        gettimeofday(&tv_start, NULL);
        start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
    }

    struct timeval tv_now;
    gettimeofday(&tv_now, NULL);
    uint64_t now_ms;
    now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;

    uint32_t time_ms = now_ms - start_ms;
    return time_ms;
}